首页 > 解决方案 > C++ 未定义的错误引用(Visual Studio 代码)

问题描述

这是我遇到过的最奇怪的问题,一切都是正确的,并且已经过仔细审查,但程序仍然无法运行。我也在在线 IDE 上尝试了这段代码(zybooks;我大学的课程),它在那里运行得非常好。

我究竟做错了什么?

主文件

#include <iostream>

#include "Character.h"

using namespace std;

int main(){
    Character character("ekko", "black", 100, 100);
    cout << character.getName();

    return 0;
}

字符.cpp

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

Character::Character(){
    name = "Summoner";
    race = "NONRACIAL";
    level = 0;
    health = 0;
}

Character::Character(string name, string race, int level, int heatlh ){
    this->name = name;
    this->race = race;
    this->level = level;
    this->health = health;
}

void Character::setName(string name){ this->name = name; }
void Character::setRace(string race){ this->race = race; }
void Character::setLevel(int level){ this->level = level; }
void Character::setHealth(int health){ this->health = health; }

string Character::getName(){ return name; }
string Character:: getRace(){ return race; } 
int Character:: getLevel(){ return level; }
int Character:: getHealth(){ return health; }

字符.h

#include <iostream>

using namespace std;

#ifndef CHARACTER_H
#define CHARACTER_H

class Character{
private:
    string name;
    string race;
    int level, health;

      
public:
    Character();
    Character(string name, string race, int level, int heatlh );

    string getName();
    string getRace(); 
    int getLevel();
    int getHealth();

    void setName(string name);
    void setRace(string race);
    void setLevel(int level);
    void setHealth(int health);
};

#endif

错误:

[Running] cd "c:\Users\diono\OneDrive\Desktop\COSC_1430_RPG_GAME\Character\" && g++ tempCodeRunnerFile.cpp -o tempCodeRunnerFile && "c:\Users\diono\OneDrive\Desktop\COSC_1430_RPG_GAME\Character\"tempCodeRunnerFile
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): in function `main':
C:\temp\gcc\build-mingw-w64\mingw-w64-crt/../../src/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status

[Done] exited with code=1 in 0.883 seconds

标签: c++visual-studio-codeundefined

解决方案


Visual Studio Code 只是编辑器编码环境,因此您需要为其添加更多编译工具,例如 Cmake 来执行您的代码。 https://code.visualstudio.com/docs/cpp/cmake-linux#:~:text=Install%20the%20C%2FC%2B%2B%20extension,a%20debugger%2C%20and%20build%20tools


推荐阅读