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

Linker Error: Undefined reference / bad reloc address / ld returned 1 exit

本文关键字:错误 地址 定位 出口 ld 返回 1个 新定位 未定义 引用 链接      更新时间:2023-10-16

我对编程比较陌生,尤其是面向对象语言。我有类播放器,正在尝试在main中使用简单的数学函数进行测试。我注意到链接器错误,但忽略了它们,直到我在函数调用语句中走得更远,现在我有点不知道该在哪里看。我想我误解了重叠的构造函数/构造函数......

这是我的错误:

*C:UsersNytzaAppDataLocalTempccRUivW8.o   ootest.cpp:(.text+0x272): undefined reference to `Player::Player(char const*, int, int, int)'
C:UsersNytzaAppDataLocalTempccRUivW8.o    ootest.cpp:(.text+0x27e): undefined reference to `Player::Player()'
C:UsersNytzaAppDataLocalTempccRUivW8.o    ootest.cpp:(.text+0x2a5): undefined reference to `Player::Player(char const*, int, int, int)'
C:UsersNytzaAppDataLocalTempccRUivW8.o    ootest.cpp:(.text+0x2cc): undefined reference to `Player::Player(char const*, int, int, int)'
C:UsersNytzaAppDataLocalTempccRUivW8.o    ootest.cpp:(.text+0x2f6): undefined reference to `Player::Player(char const*, int, int, int)'
C:UsersNytzaAppDataLocalTempccRUivW8.o    ootest.cpp:(.text+0x320): undefined reference to `Player::Player(char const*, int, int, int)'
c:program files (x86)dev-cppmingw64x86_64-w64-mingw32binld.exe    C:UsersNytzaAppDataLocalTempccRUivW8.o: bad reloc address 0x0 in section `.pdata$_ZStanSt13_Ios_FmtflagsS_'
D:DocumentsProgramscollect2.exe  [Error] ld returned 1 exit status*

包括/玩家类

#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] = '';
}
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;
}

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

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

至少添加如下的存根定义:

Player::Player() 
{
}
Player::Player( const char [], int, int, int )
{
}
相关文章: