C++ : undefined reference issue in a simple case of separate compilation - c++

Given the below class definition in the header file - "class1.h"
#ifndef CLASS1_H
#define CLASS1_H
class class1
{
public:
class1 &fcn();
};
#endif
and the member function fcn is defined in the source file - "class1.cpp"
#include "class1.h"
#include<iostream>
inline class1 &class1::fcn()
{
std::cout << "Welcome to Class1" << std::endl;
return *this;
}
when the following code in "main.cpp" is executed
#include <iostream>
#include "class1.h"
int main()
{
class1 myclass;
myclass.fcn();
}
it produces the following error
C:\...\rough>g++ main.cpp class1.cpp && a
C:\...\Local\Temp\ccJvpsRr.o:main.cpp:(.text+0x15): undefined reference to `class1::fcn()'
collect2.exe: error: ld returned 1 exit status
What went wrong?

The inline keyword is the problem. You are supposed to use that with functions that are defined in headers. In your case, remove it and it should work fine.

Related

How to fix problems when linking cpp files?

Config.h:
#ifndef CONFIG_H
#define CONFIG_H
class Config {
public:
static int GetDefult();
};
#endif //CONFIG_H
Config.cpp:
#include "Config.h"
int Config::GetDefult(){
return 1;
}
main.cpp:
#include "Config.h"
#include<iostream>
using namespace std;
int main()
{
cout << Config::GetDefult() << endl;
}
When my main file is linking, it prompts undefined references:
/tmp/ccD7rrRH.o: In function `main':
main.cpp:(.text+0x5): undefined reference to `Config::GetDefult()'
collect2: error: ld returned 1 exit status
How to include the .h file and make the program run successfully?
Playing with the shown code (the more useful one from first versio of your question and after fixing the differences to a MRE with wild guesses),
I got mostly "is not a member of class Config".
I got it to build like below; because obviously you can fix that error by making them members of that class. I chose private because it seems appropriate, but that is not needed for the solution of your problem.
#ifndef CONFIG_H
#define CONFIG_H
#include <mutex>
using namespace std;
class Config {
public:
static Config* GetDefult();
private:
static Config* p_instance;
static mutex mutex_;
};
#endif //CONFIG_H
//Config.cpp
#include "Config.h"
Config* Config::GetDefult()
{
return Config::p_instance; // just to fix building, wild guess
}
Config* Config::p_instance;
mutex Config::mutex_;
However, please do not using namespace std; in a header.

Including header files in C++ (class definition and method implementation)

I have already checked StackOverflow to find the solution to my problem, but I think I might be missing something. I am trying to define a class in a header file (.h) and implement its methods in a cpp file (.cpp), but it does not work.
main.cpp:
#include <iostream>
#include "Message.h"
using namespace std;
int main()
{
Message *t = new (Message);
t->display();
return 0;
}
Message.h:
#ifndef MESSAGE_H_INCLUDED
#define MESSAGE_H_INCLUDED
class Message {
public:
void display();
};
#endif // MESSAGE_H_INCLUDED
Message.cpp:
#include "Message.h"
void Message::display() {
cout << "Hello!";
}
I don't understand why I keep getting the following error
undefined reference to 'Message::display()'
Compile this with the command g++ -std=c++11 Message.cpp main.cpp

c++ 'undefined reference to' error

I'm having one of those "undefined reference to " errors when compiling a c++ program. I know this is common pitfall, but so far was unable to figure out what I'm doing wrong.
Here's the relevant code. Ex1Two_Sum.h:
#ifndef EX1TWO_SUM_H
#define EX1TWO_SUM_H
#include <vector>
using namespace std;
namespace ddc {
class Ex1Two_Sum
{
public:
void f();
protected:
private:
};
}
#endif
Ex1Two_Sum.cpp:
#include <vector>
#include <cstddef>
#include <iostream>
using namespace std;
namespace ddc {
class Ex1Two_Sum {
public:
void f(){
cout << "works" << endl;
}
};
}
And finally, main.cpp:
#include <iostream>
#include "Ex1Two_Sum.h"
using namespace std;
using namespace ddc;
int main()
{
Ex1Two_Sum ex1;
ex1.f();
return 0;
}
I compile as follows:
g++ -std=c++11 -c Ex1Two_Sum.cpp
g++ -std=c++11 -c main.cpp
g++ Ex1Two_Sum.o main.o
yielding the following message:
main.o: In function `main':
main.cpp:(.text+0x2c): undefined reference to `ddc::Ex1Two_Sum::f()'
collect2: error: ld returned 1 exit status
Your source file redefines the whole class, with an inline function definition, when it just needs to provide a non-inline function definition.
#include "Ex1Two_Sum.h"
void ddc::Ex1Two_Sum::f() {
std::cout << "should work\n";
}
Also, please don't put using namespace std; in a header. Not everyone wants the global namespace polluted in potentially surprising ways.
First, which line of the command throws that error?
Second, I think you forgot to include the Ex1Two_Sum.h in the Ex1Two_Sum.cpp
Third you need to change class ....... in Ex1Two_Sum.cpp to:
void Ex1Two_Sum::f(){...}

Error in including a Global Vector when used in multiple files

// Main.cpp
#include <iostream>
#include "common.h"
#include "second.cpp"
#include <vector>
int main(){
global = 10;
ip.push_back("TestTest");
std::cout << global << std::endl;
TestClass t;
t.print();
}
//common.h
#ifndef GLOBAL_H
#define GLOBAL_H
#include <vector>
#include <string>
extern int global;
extern std::vector<std::string> ip ;
#endif
// second.cpp
#include <iostream>
#include "common.h"
int global;
class TestClass{
public:
void print();};
void TestClass::print(){
global++;
std::cout << "Global: "<<global << std::endl;
std::cout << "IP String: "<<ip[0] << std::endl;
}
// Console Error
ubuntu:deleteme$ g++ main.cpp
/tmp/ccoJpYRl.o: In function `TestClass::print()':
main.cpp:(.text+0x55): undefined reference to `ip'
/tmp/ccoJpYRl.o: In function `main':
main.cpp:(.text+0xdd): undefined reference to `ip'
collect2: error: ld returned 1 exit status
The above works when I am just using with the int global variable. However when I added a vector ip to the common.h I am getting the showed error.
This seems like a elemental thing but couldn't get an answer.
Thanks in advance :)
You didn't define the std::vector<std::string>.
With extern you just declard that it's global but defined in another place.
You should add the definition under your int global in second.cpp
// second.cpp
#include <iostream>
#include "common.h"
int global;
std::vector<std::string> ip;
class TestClass{
As an aside, you shouldn't use globals.

includes inside a namespace

Is the following approach correct? Well i get a compilation error.
a.hpp is
#include <iostream>
class a
{
public:
void classa_f();
};
a.cpp is
#include "a.hpp"
void a::classa_f()
{
std::cout<< "a::classa_f\n";
}
main.cpp
#include <iostream>
namespace myname {
#include "a.hpp"
}
int main ()
{
myname::a obj;
obj.classa_f();
return 0;
}
I get the following error
g++ main.cpp a.o
/tmp/ccOOf5s7.o: In function main':
main.cpp:(.text+0x11): undefined reference tomyname::a::classa_f()'
collect2: ld returned 1 exit status
Well my question is, is it possible to have just the includes under the namespace but not the actual implementation, because I can see that compiler is searching the namespace for he definition of the function.which is actually not there.
namespace myname {
#include "a.hpp"
}
Declares a class method myname::a::classa_f , which obviously doesn't exist in your program. It's not valid.
In the implementation, you must
namespace myname
{
void a::classa_f()
{
std::cout<< "a::classa_f\n";
}
}
and please remove #include <iostream> from the hpp file, it gets imported into the namespace too.