how could I link the implementation file to the header file and to the main function.

I made this program;

// This is my implementation file.. (imp.cpp)
#include <iostream>

using namespace std;

#include "CRectangle.hpp"

output()
{
cout<< "hello";
return 0;
}


// This is my header file..
#include <iostream>

using namespace std;

class CRectangle
{
public:
void output();
};


// This is my main function
#include <iostream>

using namespace std;

#include "CRectangle.hpp"

int main(void)
{
CRectangle A;
A.output();
}


:confused: The error is this:

/tmp/ccmKOGOR.o: In function `main':main.cpp:(.text+0x23): undefined reference to `CRectangle::output()'
collect2: ld returned 1 exit status


please help me... thanx in advance...

Dani AI

Generated

Two things cause the “undefined reference to CRectangle::output” linker error: the member function either wasn’t defined with the exact class signature the header declares, or the .cpp that contains the definition wasn’t compiled/linked into the final program. ’s post shows both problems — the implementation is missing the return type and the CRectangle:: scope, and the build step must include that implementation file. ’s note about adding files to a project/makefile and ’s clarification about ensuring all .cpp files are part of the build are both correct and complementary.

A minimal, correct implementation (in the .cpp) should define the member with the class qualifier and match the header’s signature:

#include "CRectangle.hpp"
#include <iostream>

void CRectangle::output()
{
    std::cout << "hello\n";
}

Keep #include <iostream> and std::cout in the .cpp; avoid putting using namespace std; in headers.

The header should declare the class and use include guards to prevent multiple-definition issues:

#ifndef CRECTANGLE_HPP
#define CRECTANGLE_HPP

class CRectangle {
public:
    void output();
};

#endif // CRECTANGLE_HPP

Finally, make sure both source files are compiled and linked. From the command line with g++:

g++ -Wall -Wextra main.cpp imp.cpp -o myprog

or with separate compilation:

g++ -c imp.cpp
g++ -c main.cpp
g++ imp.o main.o -o myprog

Key checks: signatures must match exactly (return type and name), the definition must use CRectangle::output, and the implementation file must be included in the build.

Recommended Answers

All 2 Replies

Member Avatar for Member #114696

To include your own header file, your need to build a project or makefile.

>To include your own header file, your need to build a project or makefile.
Most compilers don't require that header files are specified in makefiles/command arguments, since #include specifies where the file can be found.

More importantly, ensure that your source (.cpp) files are all added to your project/makefile, and the lack of them will cause the linker errors the OP was experiencing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.