c++ 'undefined reference to' error - c++

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(){...}

Related

template class and free function namespace problems with implementation

I have a class defined like this (trivial includes and preprocessor wrappers, etc. are omitted, but they are there):
In Matrix.h
namespace matrixmanip {
template<typename T>
class Matrix {
public:
void someFunc() const;
//...
};
template<typename T>
void Matrix<T>::someFunc() const {
//...
}
} // namespace matrixmanip
In bitTransform.h
#include "Matrix.h"
namespace matrixmanip {
Matrix<char> bitExpand(const Matrix<char> &mat);
} // namespace matrixmanip
In bitTransform.cpp
#include "bitTransform.h"
using namespace matrixmanip;
Matrix<char> bitExpand(const Matrix<char> &mat) {
mat.someFunc();
//...
}
In tester.cpp
#include "Matrix.h"
#include "bitTransform.h"
matrixmanip::Matrix<char> A ... // construct a character matrix, details of which are unimportant here
matrixmanip::Matrix<char> B = matrixmanip::bitExpand(A);
However, when I compile and link like this:
g++ -c tester.cpp
g++ -c bitTransform.cpp
g++ -Wall -O2 tester.o bitTransform.o -o tester
I get an undefined reference error, specifically
/tmp/ccateMEK.o: In function `main':
tester.cpp:(.text+0xbf9): undefined reference to `matrixmanip::bitExpand(matrixmanip::Matrix<char> const&)'
collect2: error: ld returned 1 exit status
Why am I getting this error? It seems to me that my namespace resolution is okay and my linkage is fine...
bitExpand defines a free function in global namespace, not in matrixmanip namespace. using directive does not move definitions into the namespace being used. You should put definition in proper namespace directly:
namespace matrixmanip
{
Matrix<char> bitExpand(const Matrix<char> &mat)
{
mat.someFunc();
//...
}
}

C++ undefined namespace

I'm trying to create a header file, implementation file and main file for a simple c++ program that returns the systems hostname using gethostname().
my header file that declares my class, data, and methods
//hostname.h
#pragma once
namespace hostnamespace
{
class hostname_class
{
public:
hostname_class();
void to_string();
private:
char *name;
};
}
my implementation file that defines my class and methods
//hostname_class.cpp
#include "hostname.h"
#include <iostream>
#include <unistd.h>
#include <limits.h>
using namespace hostnamespace;
using namespace std;
class hostname_class{
private:
char *name;
public:
hostname_class(){
gethostname(name, HOST_NAME_MAX);
}
void to_string(){
cout << "Hostname:" << &name << endl;
}
};
my main program file
//hostname_main.cpp
#include "hostname.h"
#include <iostream>
using namespace hostnamespace;
int main() {
hostname_class host;
host.to_string();
return 0;
}
when I try and run g++ -o main hostname_main.cpp hostname_class.cpp
I get this error
/bin/ld: /tmp/ccGfbyuu.o: in function `main':
hostname_main.cpp:(.text+0x1f): undefined reference to `hostnamespace::hostname_class::hostname_class()'
/bin/ld: hostname_main.cpp:(.text+0x2b): undefined reference to `hostnamespace::hostname_class::to_string()'
collect2: error: ld returned 1 exit status
any help would be appreciated.
You are redeclaring the hostname_class in the global namespace instead of defining it within the namespace you intended.
A sample structure of how your hostname_class.cpp file could be written.
#include <iostream>
#include <unistd.h>
#include <limits.h>
#include "hostname.h"
using namespace std;
namespace hostnamespace
{
hostname_class::hostname_class() {
gethostname(name, HOST_NAME_MAX);
}
void hostname_class::to_string() {
cout << "Hostname:" << name << endl;
}
};
One side note - I literally copied your method implementations verbatim. It will compile. But name is certainly uninitialized and pointing to an undefined memory address when you pass it into gethostname. That's probably not good.

Ttrying to understand Classes and headers

**PROBLEM SOLVED. It appears that i had created an extra header by mistake, and since i deleted him , it worked. **
So i am trying to understand about classes and headers and how i can make them work together.
I am following an online tutorial but it seems that something is going wrong in my code.
The problem is that when i try to run the main it gives me this error:
multiple definition of Cat::speak() and all the other functions.
main.cpp
#include <iostream>
#include "class.h"
using namespace std;
int main()
{
Cat jim;
jim.makehappy();
jim.speak();
Cat george;
george.makesad();
george.speak();
return 0;
}
class.cpp
#include <iostream>
#include "class.h"
using namespace std;
void Cat::speak()
{
if (happy)
{
cout << "meoww" << endl;
}
else
{
cout << "sssss" << endl;
}
}
void Cat::makehappy()
{
happy = true;
}
void Cat::makesad()
{
happy = false;
}
class.h
#ifndef CLASS_H_INCLUDED
#define CLASS_H_INCLUDED
class Cat
{
private:
bool happy;
public:
void makehappy();
void makesad();
void speak();
};
#endif // CLASS_H_INCLUDED
From what you have shown here there should be no problems. What you could do to temporarily resolve this to find out if you are actually defining this function in several places is to wrap your class in a namespace.
class.h
#ifndef CLASS_H_INCLUDED
#define CLASS_H_INCLUDED
namespace myNamespace {
class Cat {
private:
bool happy;
public:
void makehappy();
void makesad();
void speak();
};
} // namespace myNamespace
#endif // CLASS_H_INCLUDED
class.pp
#include <iostream>
#include "class.h"
// using namespace std; // Don't Use - This is Bad Practice
// Can cause name clashing when trying to resolve name lookup
namespace myNamespace {
void Cat::speak() {
if (happy) {
std::cout << "meoww" << std::endl;
} else {
std::cout << "sssss" << std::endl;
}
}
void Cat::makehappy() {
happy = true;
}
void Cat::makesad() {
happy = false;
}
} // namespace myNamespace
main.cpp
#include <iostream>
#include "class.h"
// using namespace std; // Again -Bad Practice
int main() {
using namespace myNamespace;
Cat jim;
jim.makehappy();
jim.speak();
Cat george;
george.makesad();
george.speak();
return 0;
}
Try this to see if you are getting the same compiler error. This should help you to see if you are defining this function in multiple spaces. Also by removing the using namespace std; and just using the scope resolution operator to the std:: namespace will help to eliminate any possible problems and any possible future problems.
How are you compiling the code? You need to make sure that you are building the specific "class.o" and "main.o" files separately before linking them together. Here is an example Makefile.
all: main
main: main.o class.o
g++ main.o class.o -o main
main.o: main.cpp class.h
g++ -c main.cpp
class.o: class.cpp class.h
g++ -c class.cpp
It looks like you are using double inclusion guards so I don't think that is the problem. Check out this answer for a more in-depth explanation of what is happening: Error with multiple definitions of function

C++ Simple compile error

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

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.