Quinn McHugh Quinn McHugh -3 years ago 111
C++ Question

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.


#include <iostream>
#include "Player.h"

using namespace std;

int main()
Player rob;
cout << "Iran" << endl;
return 0;


#ifndef PLAYER_H
#define PLAYER_H

class Player {
int score;



#include "Player.h"

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.

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download