簡體   English   中英

編譯基本C ++類

[英]Compiling Basic C++ Class

我正在嘗試編譯一個非常基本的C ++程序,但是有些錯誤。 不管是什么,我相信這是非常明顯的。 我有三個非常短的文件。

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;
}

我用來編譯的命令是g++ main.cpp -o main我編譯器發出的錯誤是:

/tmp/ccexA7vk.o: In function `main':
main.cpp:(.text+0x10): undefined reference to `Player::Player()'
collect2: error: ld returned 1 exit status

注意:所有這些文件都在同一目錄中。

正如評論中所提到的,您沒有將Player.cpp提供給編譯器。 您應該將所有cpp文件提供給編譯器。

g++ main.cpp Player.cpp -o main

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM