Simple class usage+undefined reference - c++

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.

Related

Simple multifile c++11 compilation g++ when defining personal namespace in classes

Working with a Fedora g++, I've noticed something anoying for me.
I think I misundertands g++ behaviors with namesapce and header files.
You will see 2 versions of a very simple Class and a main function.
First one is fully embeded in single file named "mainfull.cpp" and compiles silently.
Second one is the same code distributed in three files "MaClass.hpp", "MaClass.cpp" and "main.cpp". The issue come from this version.
$ g++ -o doit MaClass.cpp main.cpp
/usr/bin/ld : /tmp/cc9IOJE8.o : in function « main » :
main.cpp:(.text+0x10) : undefined reference to « test::example::MaClass::doit() »
collect2: error: ld returned exit status 1
But the error vanishes with namespace suppression or when changing the include from the header file to the implementation file.
Here are my questions :
What is the good way to include multifiles classes with namespaces ?
How should one uses g++ in that case ?
But I am a chatterbox ; here is the code :
mainfull.cpp : no pb when compiling with : g++ -o mainfull mainfull.cpp
#include <iostream>
namespace test {
namespace example {
class MaClass {
public:
void doit();
};
}
}
int main() {
test::example::MaClass c;
c.doit();
return 0;
}
namespace test {
namespace example {
void MaClass::doit() {
::std::cout << "I did it !" << ::std::endl;
}
}
}
And here the version on witch I get doubts: (the whole remaining files)
MaClass.hpp
#ifndef MACLASS
#define MACLASS
#include <iostream>
namespace test {
namespace example {
class MaClass {
public:
void doit();
};
}
}
#endif
MaClass.cpp
#include "MaClass.hpp"
namespace test {
namespace example {
void MaClass::doit() {
::std::cout << "I did it !" << ::std::endl;
}
}
}
main.cpp
#include "MaClass.hpp"
int main() {
test::example::MaClass c;
c.doit();
return 0;
}
The issue occurs with :
g++ -o doit MaClass.cpp main.cpp -std=c++11
I don't know the reason of that. I know that the problem come from me and not from Fedora.
Any help would be apreciated.
I have no problem on Visual Studio 2019 neither with the one nor the other version ; everything is automatic, I should have miss something.

Why does namespace usage break MinGW compilation?

Through the act of separating a set of input and output related functions from other parts of a program, I have encountered a problem with compiling files when functions in a header are placed within a namespace. The following files compile:
main.cpp
#include "IO.h"
int main()
{
testFunction("yikes");
}
IO.h
#ifndef IO_H_INCLUDED
#define IO_H_INCLUDED
#include <string>
void testFunction(const std::string &text);
#endif
However, when testFunction is placed in a namespace:
#ifndef IO_H_INCLUDED
#define IO_H_INCLUDED
#include <string>
// IO.h
namespace IO
{
void testFunction(const std::string &text);
}
#endif
within IO.h, and then invoked as IO::testFunction, compilation fails, throwing
undefined reference to `IO::testFunction(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
collect2.exe: error: ld returned 1 exit status`
In each case, IO.cpp is
#include <string>
#include <iostream>
void testFunction(const std::string &text)
{
std::cout << text << std::endl;
}
and the compilation command is g++ -std=c++11 main.cpp IO.cpp, with the compiler being x86_64-w64-mingw32 from TDM-GCC, on Windows 10 Home.
If you change the declaration of your function to be in a namespace you need to implement the function within this namespace aswell.
The signature of your function will be IO::testFunction(...) but you only implemented testFunction(...) so there is no implementation for IO::testFunction(...)
The header file (IO.h):
namespace IO {
void testFunction(const std::string &text);
}
The cpp file (IO.cpp):
#include "IO.h"
// either do this
namespace IO {
void testFunction(const std::string &text) { ... }
// more functions in namespace
}
// or this
void IO::testFunction(const std::string &text) { ... }

Compiling Basic C++ Class

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

Linker can't find function definition in a namespace

I get this /tmp/ccnL7Yz1.o: In function 'main':
main.cpp:(.text+0x70): undefined reference to 'dng::genDungeon()'
main.cpp:(.text+0xf0): undefined reference to 'dng::clrDungeon(char**)'
collect2: error: ld returned 1 exit status error when I'm trying to compile my program. It worked great before I added namespace functions. I'm compiling it like this: g++ -std=c++11 main.cpp Dungeon.cpp
Dungeon.h
namespace dng {
char** genDungeon();
void clrDungeon(char**);
class Dungeon {
//Methods and variables
}
}
Dungeon.cpp
#include "Dungeon.h"
using namespace dng;
char** genDungeon()
{
//Stuff
}
void clrDungeon(char** dungeon)
{
//Another Stuff
}
/*Implementation of class methods
void Dungeon::genStart(){} -> like this */
main.cpp
#include "Dungeon.h"
int main ()
{
//Stuff
auto dungeon = dng::genDungeon();
//Stuff
dng::clrDungeon(dungeon);
return 0;
}
I also tried to make .o files by myself g++ -std=c++11 -c main.cpp g++ -std=c++11 -c Dungeon.cpp and then link them, but got the same error. What can be the problem?
Enclose the function definitions in the namespace dng where they are declared.
#include "Dungeon.h"
namespace dng
{
char** genDungeon()
{
//Stuff
}
void clrDungeon(char** dungeon)
{
//Another Stuff
}
//...
}
Or use qualified names.
include "Dungeon.h"
using namespace dng;
//...
char** dng::genDungeon()
{
//Stuff
}
void dng::clrDungeon(char** dungeon)
{
//Another Stuff
}
Otherwise, the functions are defined in the global namespace, and as a result, you declared four functions: two in the namespace dng and another two in the global namespace.

creating classes link error

I'm using Dev-C++ 5.2.0.1
I took an example of how to put a class in another file from a website but it resulted in an error.
In the file class.h I have:
class MyClass
{
public:
void foo();
int bar;
};
In the file class.cpp I have:
#include "class.h"
void MyClass::foo()
{
cout<< "test";
}
In the file main.cpp I have:
#include "class.h"
using namespace std;
int main()
{
MyClass a;
a.foo();
return 0;
}
Here is the error I get:
[Linker error] C:\Users\Matthew\AppData\Local\Temp\cccWe7ee.o:main.cpp:(.text+0x16): undefined reference to `MyClass::foo()'
collect2: ld returned 1 exit status
Is there something I'm doing wrong?
New answer.
Are you compiling and linking all of your files together? In gcc you would do something like:
gcc -o myExe class.cpp main.cpp
I'm not so sure about dev-c++, but I imagine it's not much different.
foo() only have definition. If you want to use a function that doesn't have a implement, Linker will give you this error "undefined reference".