Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8858195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:46:22+00:00 2026-06-14T14:46:22+00:00

I have written a very simple class in C++, namely it is the Rectangle

  • 0

I have written a very simple class in C++, namely it is the Rectangle class from http://www.cplusplus.com/doc/tutorial/classes/. In particular here’s the content of the Header file (Rectangle.h):

#ifndef RECTANGLE_H
#define RECTANGLE_H

class Rectangle {

private:
    double m_x;
    double m_y;

public:
    Rectangle();
    Rectangle(double, double);
    void setXY(double, double);
    double getArea();
};

#endif

Here is the implementation (Rectangle.cpp):

#include "Rectangle.h"

Rectangle::Rectangle() {
    setXY(1, 1);
}

Rectangle::Rectangle(double x, double y) {
    setXY(x, y);
}

void Rectangle::setXY(double x, double y) {
    m_x = x;
    m_y = y;
}

double Rectangle::getArea(void) {
    return m_x * m_y;
}

Now, I’m supposed to include the Header of Rectangle in my main class, that is:

#include <stdlib.h>
#include <iostream>
#include "Rectangle.h"

using namespace std;

int main(void) {
    Rectangle a;
    cout << "Area : " << a.getArea() << "\n";
    return EXIT_SUCCESS;
}

But, then I get the error:

make all 
g++ -O2 -g -Wall -fmessage-length=0   -c -o Chung1.o Chung1.cpp
g++ -o Chung1 Chung1.o 
Chung1.o: In function `main':
/home/chung/eclipse_ws/Chung1/Chung1.cpp:8: undefined reference to `Rectangle::Rectangle()'
/home/chung/eclipse_ws/Chung1/Chung1.cpp:9: undefined reference to `Rectangle::getArea()'
collect2: ld returned 1 exit status
make: *** [Chung1] Error 1

The error is resolved if I include the file Rectangle.cpp instead. (I’m running on Eclipse)

Am I supposed to include the CPP file instead after all?

Here’s my Makefile:

CXXFLAGS =  -O2 -g -Wall -fmessage-length=0    
OBJS =      Chung1.o    
LIBS =    
TARGET =    Chung1    
$(TARGET):  $(OBJS)
    $(CXX) -o $(TARGET) $(OBJS) $(LIBS)    
all:    $(TARGET)    
clean:
    rm -f $(OBJS) $(TARGET)    
run:    $(TARGET)   
        ./$(TARGET)

How can I modify it to compile the Rectangle class as well?

Solution: According to the answer from the user v154c1, it is necessary to compile individual cpp files and then include their headers in the main file or in any other file where this functionality is needed. Here’s any example Makefile to do so:

CXXFLAGS =      -O2 -g -Wall -fmessage-length=0
#List of dependencies...
OBJS =          Rectangle.o Chung1.o
LIBS =
TARGET =        Chung1
$(TARGET):      $(OBJS)
        $(CXX) -o $(TARGET) $(OBJS) $(LIBS)
all:    $(TARGET)
clean:
        rm -f $(OBJS) $(TARGET)
run:    $(TARGET)
        ./$(TARGET)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:46:23+00:00Added an answer on June 14, 2026 at 2:46 pm

    You are not compiling and linking the Rectangle class.

    Your compilation should look like:

    g++ -O2 -g -Wall -fmessage-length=0   -c -o Chung1.o Chung1.cpp
    g++ -O2 -g -Wall -fmessage-length=0   -c -o Rectangle.o Rectangle.cpp
    g++ -o Chung1 Chung1.o Rectangle.o
    

    If you’re using Makefile, then just add the Rectangle.cpp the same way you use Chung1.cpp. The same goes for any IDE you may be using.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple DLL written in unmanaged C++ that I access from
I've written a very simple python numpy code. It have a strange behavior... from
I have written very very simple console application which supports some command line options.
I have written a very simple CTE expression that retrieves a list of all
I have written a very simple application to update my twitter status on a
I have written a very simple control. C# Visual Studio 2008. Its output should
I have written a very simple WCF service, that worked fine (code below), then
I have written a very very very simple!! script in php. header redirection not
I have written a very simple function: function editCategory() { $ID = urlencode($_GET['id']); $cname
I have written a very simple application tha accepts batches of telemetry log rows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.