I'm trying to compile a very basic C++ program and there is something wrong. Whatever it is, I'm sure it's very obvious. I have three very short files.
main.cpp:
#include <iostream>
#include "Player.h"
using namespace std;
int main()
{
Player rob;
cout << "Iran" << endl;
return 0;
}
Player.h
#ifndef PLAYER_H
#define PLAYER_H
class Player {
public:
Player();
private:
int score;
};
#endif
Player.cpp
#include "Player.h"
Player::Player(){
score = 0;
}
The command I'm using to compile is g++ main.cpp -o main
And the error I'm being issued by the compiler is:
/tmp/ccexA7vk.o: In function `main':
main.cpp:(.text+0x10): undefined reference to `Player::Player()'
collect2: error: ld returned 1 exit status
Note: All these files are in the same directory.
As mentioned in the comments, you are not feeding Player.cpp into compiler. You should give all the cpp files to the compiler.
g++ main.cpp Player.cpp -o main
Related
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.
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.
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(){...}
I have just created this simple class.When I compile, I am getting the following error.
caller.o: In function main':
caller.cpp:(.text+0x15): undefined reference toReader::Reader(int)'
collect2: ld returned 1 exit status
Reader.h
#ifndef READER_H
#define READER_H
class Reader
{
private:
int m_month;
Reader() {}
public:
Reader(int month);
void SetDate(int month);
int GetMonth() {return m_month;}
};
#endif
Reader.cpp
#include "Reader.h"
Reader::Reader(int month);
{
SetDate(month);
}
void Reader::SetDate(int month)
{
m_month=month;
}
main program
#include <iostream>
using namespace std;
#include "Reader.h"
int main()
{ int i;
i=5;
Reader rd(i);
i=rd.GetMonth();
cout<<i;
return 0;
}
There is a ; semicolon that should not be there.
Reader::Reader(int month)//; remove semicolon from this line !!!
{
SetDate(month);
}
You need to compile all your source files into object files, and then link the object files together to produce the program.
This can be done in one step:
gcc Reader.cpp main.cpp
Or in two separate steps:
gcc -c Reader.cpp main.cpp
gcc Reader.o main.o
Of course, you should normally have an IDE, Makefile or buildsystem generator (such as CMake) take care of this for you.
I am having a Compile issue.
I have one Class
I have one header file
And of course Main to test my work.
But I am getting compile error, it is out of my understanding what I am doing wrong.
Header File:
#ifndef AGENT_H
#define AGENT_H
using namespace std;
class Agent
{
public:
Agent(string);
virtual ~Agent();
private:
string name;
};
#endif /* AGENT_H */
Agent Class (Agent.cpp)
#include "Agent.h"
using namespace std;
Agent::Agent(string _name)
{
this->name = _name;
}
Agent::~Agent()
{
delete this->name;
}
And my Main:
#include <cstdlib>
#include <iostream>
#include "Agent.h"
using namespace std;
int main(int argc, char** argv)
{
Agent agent1("Danila");
return 0;
}
So I am getting such strange error:
undefined reference to `Agent::Agent(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/main.cpp:17: undefined reference to `Agent::~Agent()'
/main.cpp:17: undefined reference to `Agent::~Agent()'
Could you guys help me understand whats wrong there?
You need an #include <string> in your header file.
Also, for good practice, keep the using namespaces in your .cpp files, if any.
You compiled without telling the compiler about Agent.cpp. I.e. you need something like this, for g++:
$ g++ main.cpp Agent.cpp -o myprogram