2012-09-05 92 views
0

首先我知道还有其他问题基本上与这一个相同,但没有一个答案似乎适用于我。我对C++和编程一般都很陌生,所以请尽可能简单地描述一下,谢谢。错误:表达必须有班级类型

所以我试图做一个简单的文本游戏,我有一对夫妇的文件,但是当我尝试使用类从一个方法它会导致错误,说该表达式必须有一个类类型。

这是代码。

的main.cpp

#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; 
} 

Warrior.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(); 
}; 

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() 
{} 
+7

保持简单,水手:我们真的需要在这个代码示例中看到你的开始屏幕吗?保持它的修剪,保持精益,并且游客变得更加敏锐。 –

+0

@KerrekSB很抱歉。有点新在这里。谢谢你的提示。 – katana7070

+0

“anumber:”标签的原因是什么? –

回答

4

这条线在main()

Warrior your_warrior(int health , int armor , int weapon); 

看起来像你声明一个函数,而不是创造Warrior类的一个实例。你应该用你的变量像这样的一些具体值调用它来创建一个

Warrior your_warrior(10,32,2); 

甚至更​​好的创建一些变量,其值设置,并传递给函数。然后致电

your_warrior.warrior_name_function(); 

您的编译错误是因为它不能识别your_warrior作为类实例,而是作为函数的声明。

+0

非常感谢,这让我疯狂。 – katana7070

0

好像你忘了小括号调用的名称功能时:

your_warrior.warrior_name_function(); 

我也建议你简单地移除Warrior类的析构函数:它没有任何要清理的东西。

+0

仍然没有解决它。感谢你如此快速,也感谢破坏者提示。 – katana7070

0
Warrior your_warrior(int health , int armor , int weapon); 

这行声明的函数named your_warrior称取int类型的三个参数,并返回Warrior类型的对象。

如果您删除三个int s它会更好。 <g>

而且,当然,在下一行添加函数调用的括号。

+0

,导致我的错误。 – katana7070

+0

是的,我假设,没有看,你已经定义了这三个名字的变量。正如@ mathematician1975建议的那样,如果需要,可以使用文字值调用该构造函数。 –

+0

无论如何感谢您的建议,我真的很感谢帮助。 – katana7070

相关问题