首先,我知道还有其他问题与这个问题基本相同,但似乎没有一个答案对我有用。我对 c++ 和一般编程很陌生,所以请尽可能简单地描述一下,谢谢。
因此,我正在尝试制作一个简单的文本游戏,并且我有几个文件,但是当我尝试使用类中的方法时,会导致错误,指出表达式必须具有类类型。
这是代码。
主文件
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include "Warrior.h"
using namespace std;
//main function
int main (void)
{
//title screen
cout<< " _____ _ _ _____ \n";
cout<< "| __|_|_____ ___| |___ | __ |___ ___ \n";
cout<< "|__ | | | . | | -_| | -| . | . |\n";
cout<< "|_____|_|_|_|_| _|_|___| |__|__| _|_ |\n";
cout<< " |_| |_| |___|\n";
cout<< "\n\n Enter any # to start \n ";
int start;
anumber:
cin>> start;
if (start < 0 || start > 0)
{
cout<< "\nWelcome to Sam Acker's simple rpg game!\n";
}
Warrior your_warrior(int health , int armor , int weapon);
your_warrior.warrior_name_function; //This is the line with the error
int exit;
cin>> exit;
return 0;
}
战士.h
#include <string>
#include <iostream>
class Warrior
{
private:
int health;
int weapon;
int armor;
std::string warrior_name;
public:
int attack();
int warrior_name_function();
Warrior(int health , int weapon , int armor);
~Warrior();
};
战士.cpp
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include "Warrior.h"
int Warrior::warrior_name_function()
{
std::cout<< "What would you like to name you warrior?\n";
std::cin>> Warrior::warrior_name;
return 0;
}
int Warrior::attack()
{
return 0;
}
Warrior::Warrior(int health , int armor , int weapon)
{
health == 100;
armor == 1;
weapon == 16;
}
Warrior::~Warrior()
{}