Reputation: 43
First of all i know there are other questions that are essentially the same as this one but none of the answers seem to work for me. I'm pretty new to c++ and programming in general so please describe as simply as possibly, thank you.
So I'm trying to make a simple text game and i have a couple files but when i try to use a method from a class it causes an error that says the expression must have a class type.
Here's the code.
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()
{}
Upvotes: 0
Views: 15042
Reputation: 21351
This line in main()
Warrior your_warrior(int health , int armor , int weapon);
looks like you are declaring a function, not creating an instance of class Warrior
. You should call it with some concrete values of your variables like this to create one
Warrior your_warrior(10,32,2);
or even better create some variables, set their values and pass to the function. Then call
your_warrior.warrior_name_function();
your compilation error is because it does not recognise your_warrior as a class instance but as a declaration of a function.
Upvotes: 4
Reputation: 76245
Warrior your_warrior(int health , int armor , int weapon);
This line declares a function named your_warrior
that take three arguments of type int
and returns an object of type Warrior
.
If you remove the three int
s it will work much better. <g>
And, of course, add the parentheses for the function call in the next line.
Upvotes: 0
Reputation: 234364
Seems like you forgot the parentheses when calling the name function:
your_warrior.warrior_name_function();
I would also suggest that you simply remove the destructor for the Warrior
class: it doesn't have anything to clean up.
Upvotes: 0