链接器错误:未定义的引用/错误的重定位地址/ ld返回1退出

时间:2014-11-21 21:24:31

标签: c++ constructor reference linker undefined

我对编程尤其是面向对象语言相对较新。我有类Player并且我试图在main中使用简单的数学函数来测试。我注意到链接器错误但忽略了它们,直到我进一步调用函数调用语句,现在我有点迷失在哪里看。我想我总是误解重叠的构造函数/构造函数...

这是我的错误:

*C:\Users\Nytza\AppData\Local\Temp\ccRUivW8.o   ootest.cpp:(.text+0x272): undefined reference to `Player::Player(char const*, int, int, int)'

C:\Users\Nytza\AppData\Local\Temp\ccRUivW8.o    ootest.cpp:(.text+0x27e): undefined reference to `Player::Player()'

C:\Users\Nytza\AppData\Local\Temp\ccRUivW8.o    ootest.cpp:(.text+0x2a5): undefined reference to `Player::Player(char const*, int, int, int)'

C:\Users\Nytza\AppData\Local\Temp\ccRUivW8.o    ootest.cpp:(.text+0x2cc): undefined reference to `Player::Player(char const*, int, int, int)'

C:\Users\Nytza\AppData\Local\Temp\ccRUivW8.o    ootest.cpp:(.text+0x2f6): undefined reference to `Player::Player(char const*, int, int, int)'

C:\Users\Nytza\AppData\Local\Temp\ccRUivW8.o    ootest.cpp:(.text+0x320): undefined reference to `Player::Player(char const*, int, int, int)'

c:\program files (x86)\dev-cpp\mingw64\x86_64-w64-mingw32\bin\ld.exe    C:\Users\Nytza\AppData\Local\Temp\ccRUivW8.o: bad reloc address 0x0 in section `.pdata$_ZStanSt13_Ios_FmtflagsS_'

D:\Documents\Programs\collect2.exe  [Error] ld returned 1 exit status*

包括/ Player类

#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <string.h>

using namespace std;

class Player
{
    public:
      Player();
      Player( const char [], int, int, int );

      void printPlayer();
      void printPlayer(const char[], int, int, int);

      void setName();
      void setName( const char [] );
      void changeGoals( int );
      void changeAssists( int );
      void changeRating( int );

      int getGoals();
      int getAssists();
      int getRating();

    private:
      char name[50];
      int goals;
      int assists;
      int rating;
};

在main中添加以下行后会出现错误:

int main()

int main()
{
    Player p1 ("Nytza Delirosa", 1, 1, 1);
    Player p2;
    Player p3 ("Jonathan Toews", 10, 9, 6);
    Player p4 ("Patrick Kane", 11, 10, -3);
    Player p5 ("Brandon Saad", 5, 8, 8);
    Player p6 ("Andrew Shaw", 6, 5, 6);

...
    return 0;
}

我的所有职能:

void Player::printPlayer()
{   
    int points = getGoals() + getAssists();
    cout  << name << endl <<
        "Goals: " << goals <<
        "Assists: " << assists <<
        "Points: " << points <<
        "Plus/Minus: " << showpos << rating << noshowpos << endl << endl;
}

void Player::setName()
{
    name[0] = '\0';
}

void Player::setName( const char playerName[] )
{
    strcpy(name, playerName);
}

void Player::changeGoals( int goalsScored )
{
    if (goalsScored < 0 )
        cout << "Error: Not at least 0 goals.";
    else goals++;
}

void Player::changeAssists( int assistsEarned )
{
    if (assistsEarned < 0 )
        cout << "Error: Not at least 0 assists.";
    else assists=+assistsEarned;
}

int Player::getGoals()
{
    return goals;
}

int Player::getAssists()
{
    return assists;
}

int Player::getRating()
{
    return rating;
}

1 个答案:

答案 0 :(得分:2)

显然你没有为Player定义构造函数:

Player();
Player( const char [], int, int, int );

至少添加这样的存根定义:

Player::Player() 
{
}

Player::Player( const char [], int, int, int )
{
}