'enemy' is not a class or namespace

di il
8 risposte

'enemy' is not a class or namespace

I'm back with a new problem! I was trying to use class but it seems that my class object aren't recognized. The program should roll a random number (0,1,2 or 3) and set the object monsterType (enemy) according to the case of the switch. But for reason that are far superior than my abilities it doesn't accept enemy as object, any suggestion?

monster.h
class monsterType
{
public:
	void setStat(std::string nam, int pow, int res, int dif, int sol); //Function for setting the stat to monsters
	void printStat(); //Function for printing the stat of a monster

monsterType(std::string nam, int pow, int res, int dif, int sol); //Constructor
monsterType(); //Constructor

private:
	std::string nam; //Monsters stat
	int pow;
	int res;
	int dif;
	int sol;
	
};
monsterImp.cpp
#include <string>
#include "monster.h"
#include <iostream>

void monsterType::setStat(std::string name, int str, int cos, int def, int cash) //Function for setting the stat to monsters
{
std::string nam=name;

int pow=str;
int res=cos;
int dif=def;
int sol=cash;
};

void monsterType::printStat(); //Function for printing the stat of a monster
{
cout << "nam " << "have " << "pow" << " strebght point/s, " << "res" << " toughness point/s and "
	<< "dif" << " defense point/s." << endl;
};
monsterType::monsterType(std::string nam, int pow, int res, int dif, int sol) //Constructor
	{
	if(pow<0)
	pow=0;
	if(res<=0)
	res=1;
	if(dif<0)
	dif=0;
	if(sol<0)
	sol=0;
	};

monsterType::monsterType() //Constructor
	{
	if(pow<0)
	pow=0;
	if(res<=0)
	res=1;
	if(dif<0)
	dif=0;
	if(sol<0)
	sol=0;
};
main.cpp
#include <string>
#include "monster.h"
#include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;

void setStat(string nam, int pow, int res, int dif, int sol); //monsterType functions
void printStat();

int main()
{

string name; //variables
int str;
int cos;
int def;
int cash;
monsterType enemy; //class object

srand ( time(NULL) ); 			//Random value
int numberMonster=rand() % 4;	//Random value

if(numberMonster>=0 && numberMonster<=4) //Control tower
{
switch(numberMonster) //Possible monsters
{
case 0:
name="Orc";
str=3, cos=3, def=0, cash=2;
void enemy::setStat(string name, int str, int cos, int def, int cash);
void enemy::printStat();
break;

case 1:
name="Rat";
str=1, cos=1, def=0, cash=1;
void enemy::setStat(string name, int str, int cos, int def, int cash);
void enemy::printStat();
break;

case 2:
name="Soldier";
str=2, cos=2, def=2, cash=2;
void enemy::setStat(string name, int str, int cos, int def, int cash);
void enemy::printStat();
break;

case 3:
name="Golem";
str=1, cos=3, def=2, cash=2;
void enemy::setStat(string name, int str, int cos, int def, int cash);
void enemy::printStat();
break;
};
}
else cout << "Alert: numberMonster has invalid valor!" << endl;
return 0; //The end
};

8 Risposte

  • Re: 'enemy' is not a class or namespace

    The correct form is:

    enemy.setStat(name, str, cos, def, cash);
    enemy.printStat();

    Bye
  • Re: 'enemy' is not a class or namespace

    Thank you for the answer! But now a there's a new problem. "undefined reference to" and then it point out the constructors and the function of the class. I have corrected a few other things, the code now is as follow:

    monster.h
    class monsterType
    {
    public:
    	void setStat(std::string nam, int pow, int res, int dif, int sol); //Function for setting the stat to monsters
    	void printStat(); //Function for printing the stat of a monster
    
    monsterType(std::string nam, int pow, int res, int dif, int sol); //Constructor
    monsterType(); //Constructor
    
    private:
    	std::string nam; //Monsters stat
    	int pow, res, dif, sol;
    	
    };
    monster.cpp
    #include <string>
    #include <iostream>
    
    void monsterType::setStat(std::string name, int str, int cos, int def, int cash) //Function for setting the stat to monsters
    {
    std::string nam=name;
    int pow=str;
    int res=cos;
    int dif=def;
    int sol=cash;
    };
    
    void monsterType::printStat() //Function for printing the stat of a monster
    {
    cout << "nam " << "have " << "pow" << " strenght point/s, " << "res" << " toughness point/s and "
    	<< "dif" << " defense point/s." << endl;
    };
    monsterType::monsterType(std::string nam, int pow, int res, int dif, int sol)//Constructor
    {
    cout << "A new monster appear!"<< endl;
    }
    monsterType::monsterType()//Constructor
    {
    cout << "A new monster appear!"<< endl;
    }
    main.cpp
    #include <string>
    #include "monster.h"
    #include <iostream>
    #include <cstdlib>
    #include <time.h>
    
    using namespace std;
    
    void setStat(string nam, int pow, int res, int dif, int sol); //monsterType functions
    void printStat();
    
    int main()
    {
    
    string name; //variables
    int str, cos, def, cash;
    monsterType enemy; //class object
    
    srand ( time(NULL) ); 			//Random value
    int numberMonster=rand() % 4;	//Random value
    
    if(numberMonster>=0 && numberMonster<=4) //Control tower
    {
    switch(numberMonster) //Possible monsters
    {
    case 0:
    name="Orc";
    str=3, cos=3, def=0, cash=2;
    enemy.setStat(name, str, cos, def, cash);
    enemy.printStat();
    break;
    
    case 1:
    name="Rat";
    str=1, cos=1, def=0, cash=1;
    enemy.setStat(name, str, cos, def, cash);
    enemy.printStat();
    break;
    
    case 2:
    name="Soldier";
    str=2, cos=2, def=2, cash=2;
    enemy.setStat(name, str, cos, def, cash);
    enemy.printStat();
    break;
    
    case 3:
    name="Golem";
    str=1, cos=3, def=2, cash=2;
    enemy.setStat(name, str, cos, def, cash);
    enemy.printStat();
    break;
    };
    }
    else cout << "Alert: numberMonster has invalid valor!" << endl;
    return 0; //The end
    };
  • Re: 'enemy' is not a class or namespace

    In Monster.cpp add:

    #include "monster.h"
    using namespace std;

    Bye
  • Re: 'enemy' is not a class or namespace

    
    in main.cpp
    void setStat(string nam, int pow, int res, int dif, int sol); //monsterType functions
    void printStat();
    
    remove those declarations from main. They are defined in monster.h and don't need to be re-declared.
  • Re: 'enemy' is not a class or namespace

    The problem persist even with the new corrections, so the error have to be elsewhere... but i can't really see where!
  • Re: 'enemy' is not a class or namespace

    In which line does the error persist? Can you point it?
  • Re: 'enemy' is not a class or namespace

    It seems to have problem with the functions in the switch.
    main.cpp:(.text+0x23): undefined reference to 'monsterType::monsteType()'
    C:\Users\*******\AppData\Local\Temp\ccom1CDB.o:main.cpp:(.text+0x112): undefined reference to 'monsterType::setStat(std::string, int, int, int, int)'
    C:\Users\*******\AppData\Local\Temp\ccom1CDB.o:main.cpp:(.text+0x12a): undefined reference to 'monsterType::printStat()'
    C:\Users\*******\AppData\Local\Temp\ccom1CDB.o:main.cpp:(.text+0x1ab): undefined reference to 'monsterType::setStat(std::string, int, int, int, int)'
    C:\Users\*******\AppData\Local\Temp\ccom1CDB.o:main.cpp:(.text+0x1c3): undefined reference to 'monsterType::printStat()'
    C:\Users\*******\AppData\Local\Temp\ccom1CDB.o:main.cpp:(.text+0x244): undefined reference to 'monsterType::setStat(std::string, int, int, int, int)'
    C:\Users\*******\AppData\Local\Temp\ccom1CDB.o:main.cpp:(.text+0x25c): undefined reference to 'monsterType::printStat()'
    C:\Users\*******\AppData\Local\Temp\ccom1CDB.o:main.cpp:(.text+0x2dd): undefined reference to 'monsterType::setStat(std::string, int, int, int, int)'
    C:\Users\*******\AppData\Local\Temp\ccom1CDB.o:main.cpp:(.text+0x2f5): undefined reference to 'monsterType::printStat()'
  • Re: 'enemy' is not a class or namespace

    Please include full source code. Standing to the last source your code compiles and runs fine on Visual Studio 2008.
Devi accedere o registrarti per scrivere nel forum
8 risposte