无法在 VS 上解析 LNK2019

Couldn't resolve LNK2019 on VS

编码时出现以下错误:1>giochino.obj : error LNK2019: riferimento al simbolo esterno "public: void __thiscall entity::print_Pv(int,int)" (?print_Pv@entity@@QAEXHH@Z) non risolto nella funzione _main 1>C:\Users\tomma\source\repos\giochino\Debug\giochino.exe : fatal error LNK1120: 1 esterni non risolti 1>Compilazione progetto "giochino.vcxproj" NON COMPLETATA.

我好像找不到解决办法

这是代码,虽然很短我找不到问题

我创建的库:

    #include <iostream>

using namespace std;

class entity{
public:
    int pv_left;
    int Max_pv;
    //find the percentage of life left and print a bar filled of the same percentage
    void print_Pv(int pv_now, int pv_max) {
        char pv_bar[10];
        int pv_perc = ( pv_now / pv_max) * 10;
        for (int i = 0; i < 10; i++) {
            if (i <= pv_perc) {
                pv_bar[i] = '*';
            }
            else if (i > pv_perc) {
                pv_bar[i] = '°';
            }
        }
        for (int i = 0; i < 10; i++) {
            cout << pv_bar[i];
        }
    }
};

库的header

#pragma once
#include <iostream>

class entity {
public:
    int pv_left;
    int Max_pv;
    void print_Pv(int pv_now, int max_pv);
};

和主要方法

#include "game_library.h"

using namespace std;


entity Hero;

int main()
{
    Hero.Max_pv = 100;
    Hero.pv_left = 10;
    Hero.print_Pv(Hero.pv_left, Hero.Max_pv);
}

你的实现文件有误,你需要

#include "your.h"
void entity::print_Pv(int pv_now, int pv_max) {
    char pv_bar[10];
    int pv_perc = ( pv_now / pv_max) * 10;
    for (int i = 0; i < 10; i++) {
        if (i <= pv_perc) {
            pv_bar[i] = '*';
        }
        else if (i > pv_perc) {
            pv_bar[i] = '°';
        }
    }
    for (int i = 0; i < 10; i++) {
        cout << pv_bar[i];
    }
}

你不能再声明class,只声明你没有放在.h文件中的方法体