I am new to coding in C++ and I want to make a simple Pokemon game.
I created a class in a header file and I am defining the functions in a separate .cpp file.
I also have a main file where I will run my actual code.
So I am defining a std::string function in my functions file, and it says std is an undeclared identifier.
Here are each of my files:
Function Definition:
#include "fns.hpp"
int Pokemon::getHP() {
return hp;
}
int Pokemon::getAttack() {
return attack;
}
int Pokemon::getDefense() {
return defense;
}
int Pokemon::getSpecialAttack() {
return specialAttack;
}
int Pokemon::getSpecialDefense() {
return specialDefense;
}
int Pokemon::getSpeed() {
return speed;
}
std::string Pokemon::getAttack1() {
return attack1;
}
std::string Pokemon::getAttack2() {
return attack2;
}
std::string Pokemon::getAttack3() {
return attack3;
}
std::string Pokemon::getAttack4() {
return attack4;
}
Pokemon::Pokemon(int qhp,int qdefense,int qattack,int qspecialAttack,int qspecialDefense,int qspeed,std::string qattack1,std::string qattack2,std::string qattack3,std::string qattack4)
: hp(qhp),attack(qattack),defense(qdefense),specialAttack(qspecialAttack),specialDefense(qspecialDefense),speed(qspeed),attack1(qattack),attack2(qattack2),attack3(qattack3),attack4(qattack4) {}
Function Declaration:
class Pokemon {
int hp,attack,defense,specialAttack,specialDefense,speed;
std::string attack1,attack2,attack3,attack4;
public:
int getHP();
int getAttack();
int getDefense();
int getSpecialAttack();
int getSpecialDefense();
int getSpeed();
int getAttack1();
int getAttack2();
int getAttack3();
int getAttack4();
Pokemon(int qhp,int qdefense,int qattack,int qspecialAttack,int qspecialDefense,int qspeed,std::string qattack1,std::string qattack2,std::string qattack3,std::string qattack4);
};
Whenever I say std::string, it says it is an undeclared identifier.
Can someone please help me?
It is because you have not used the library for it.
use the below at the top of your header file
#include<string>
Related
I wrote a simple c++ program and it gave me error in the setter, function definition does not declare parameters, although there was a setter above it and it worked correctly the error is in the function named setattack here is the code:
{
#include <iostream>
using namespace std;
class Warrior{
private:
string name;
int attack;
int defense;
public:
void setname(string m){
name=m;
}
string getname(){
return name;}
void setattack{int aw}{
attack=aw;
}
int getattack(){
return attack;}
void setdefense{int dw}{
defenset=dw;
}
int getdefense(){
return defense;}
void kill(const Monster &monster){
}
};
int main()
{
return 0;
}
}
In your set function you have used {} curly braces for arguments I think it should be (), and there is an extra } after main.
I get the following error message about my enum, and I can't figure out why.
I have only 1 header file and only 1 source file where I implemented all the functions (and main). I declared the enum within class "Survivor", in "public". I didn't forget to include the header file in the source file. Please help me see what I've done wrong.
"error: 'e_status' does not name a type"
This is my header file:
class Survivor
{
public:
enum e_status {SINGLE, MARRIED, RELATIONSHIP};
char* get_name();
int get_age();
e_status get_status();
void set_name(char n[]);
void set_age (int a);
void set_status (e_status s);
const void print();
private:
char name [20];
int age;
e_status status;
};
This is the relevant part in my Source file:
e_status Survivor::get_status()
{
return status;
}
You have to use a qualified name
Survivor::e_status Survivor::get_status()
{
//...
}
The type e_status is a member of the class Survivor.
A return type of a member function is not searched in the class scope while for example a parameter type is used in the class scope.
Consider the following two function definitions.
#include <iostream>
class Survivor
{
public:
enum e_status {SINGLE, MARRIED, RELATIONSHIP};
e_status get_status();
void set_status (e_status s);
//...
private:
e_status status;
};
Survivor::e_status Survivor::get_status()
{
return status;
}
void Survivor::set_status (e_status s)
{
status = s;
}
int main()
{
return 0;
}
You're trying to reference outside the class; do this:
Survivor::e_status Survivor::get_status()
I am getting the error "error: Invalid use of AppleFarmer::AppleFarmer. I do not know why I am getting this error since I am not trying to pass any input into my Constructor. Is it possible I have an issue with my .h file? What am i doing wrong to get this error?
I have three different files, and I may also be having an issue with linking the code together as I am doing #include for a .cpp file. I am not sure if my code works aside from this error, but I am stuck on this error.
appleFarmerMain.cpp
#include<iostream>
#include "appleFarmer.cpp"
int main(){
AppleFarmer m;
int harvest;
int demand;
m.AppleFarmer();
while(m.endOfMonth()==false){
cout<<"Enter a harvest amount:"<<endl;
cin>>harvest;
m.harvestApples(harvest);
cout<<"Enter a demand:"<<endl;
cin>>demand;
m.sellApples(demand);
cout<<"Apple Inventory: "<<m.getInventory()<<endl;
m.updateCurrentDay();
}
return 0;
}
appleFarmer.cpp
#include "appleFarmer.h"
#include "<iostream>
using namespace std;
AppleFarmer::AppleFarmer(){
for(int i=0;i<30;i++){
sales[i]=0;
harvest[i]=0;
}
}
bool AppleFarmer::sellApples(int demand){
if(demand<= inventory){
sales[currentDay]=demand;
inventory=inventory-demand;
}
else{
sales[currentDay]=0;
}
}
void AppleFarmer::harvestApples(int dayHarvest){
harvest[currentDay]= dayHarvest;
inventory=inventory+dayHarvest;
}
bool AppleFarmer::endOfMonth(){
if (currentDay=maxDays){
return true;
}
else{
return false;
}
}
int AppleFarmer::updateCurrentDay(){
currentDay=currentDay+1;
}
int AppleFarmer::getInventory(){
return inventory;
}
double AppleFarmer::calculateAverageHarvest(){
}
double calculateAverageSales(){
}
void AppleFarmer::printSales(){
}
void AppleFarmer::printHarvest(){
}
appleFarmer.h
#ifndef APPLEFARMER_H
#define APPLEFARMER_H
class AppleFarmer
{
public:
AppleFarmer();
bool sellApples(int);
void harvestApples(int);
bool endOfMonth();
int updateCurrentDay();
int getInventory();
double calculateAverageHarvest();
double calculateAverageSales();
void printSales();
void printHarvest();
private:
int sales[30];
int harvest[30];
int maxDays = 30;
int currentDay = 0;
int inventory = 0;
};
#endif
In C++ you don't call the constructor on an object. That happens at object creation time. The line
m.AppleFarmer();
isn't needed. The constructor is implicitly called here:
AppleFarmer m;
You need to include appleFarmer.h instead of appleFarmer.cpp because the header file (with .h extension) contains the declaration while the .cpp file contains the implementation.
Then you need also to delete m.AppleFarmer(); because the constructor is called during the declaration (AppleFarmer m text line).
I'm new to C++ programming language and it is different from Java. I tried to use functions from a header I made but when I use a function from the header , Eclipse C++ IDE says that member declaration is not found except for the constructor while it is found in the header as public.
Car.h file (header) :
#include <string>
using namespace std;
class Car {
private :
string name;
string model;
int year;
int width;
int height;
int depth;
public :
Car ();
Car (string n, string m, int y, int w, int h, int d);
void setName(string n);
void setModel (string m);
void setYear (int y);
void setSize (int w, int h, int d);
string getName ();
string getModel();
int getYear();
int getWidth();
int getHeight();
int getDepth();
};
Car.cpp file (source)
#include <iostream>
#include <string>
#include "Car.h"
using namespace std;
Car::Car(string n, string m, int y, int w, int h, int d) { //works properly
name = n;
model = m;
year = y;
width = w;
height = h;
depth = d;
}
Car::getName() { // IDE says member declaration not found
return name;
}
Car::getModel() { // IDE says member declaration not found
return model;
}
Car::getYear() { // IDE says member declaration not found
return year;
}
Car::getWidth() { // IDE says member declaration not found
return width;
}
Car::getHeight () { // IDE says member declaration not found
return height;
}
What I have did wrong ?
All of your functions are missing the return type, for example
string Car::getName() {
return name;
}
The reason why Car works is because it is a Constructor and does not need a type declaration.
All the rest of your functions do.
int Car::getYear() { // IDE says member declaration not found
return year;
}
Do this :-
#include <iostream>
#include <string>
#include "Car.h"
using namespace std;
Car::Car(string n, string m, int y, int w, int h, int d) { //works properly
name = n;
model = m;
year = y;
width = w;
height = h;
depth = d;
}
string Car::getName() { // IDE says member declaration not found
return name;
}
string Car::getModel() { // IDE says member declaration not found
return model;
}
int Car::getYear() { // IDE says member declaration not found
return year;
}
int Car::getWidth() { // IDE says member declaration not found
return width;
}
int Car::getHeight () { // IDE says member declaration not found
return height;
}
I have a simple class which I cannot instantiate and I don't know why...
Please help me !
-------Test.cpp-------
#include<iostream>
using namespace std;
#include "meteo.h"
int main()
{
Meteo meteo;
}
-------meteo.h---------
#ifndef METEO_H
#define METEO_H
class Meteo
{
public:
Meteo();
int Get(int i);
private:
char *list[];
};
#endif
-------meteo.cpp--------
#include "meteo.h"
Meteo::Meteo()
{
list[]("Sec","Venteux","Humide");
}
int Meteo::Get(int i)
{
return list[i];
}
I get the error: "undefined reference to `Meteo::Meteo()'"
It seems that the problem is that the compiler issued an error when was compiling the constructor
Meteo::Meteo()
{
list[]("Sec","Venteux","Humide");
}
and did not generate the object module.
This record
list[]("Sec","Venteux","Humide");
is invalid.
Try to change the class definition like
class Meteo
{
public:
Meteo();
int Get(int i);
private:
const char *list[3];
};
and define the constructor like
Meteo::Meteo() : list { "Sec","Venteux","Humide" }
{
}
The other reason might be that you did not include object module meteo in the project.
Take into account that this member function
int Meteo::Get(int i)
{
return list[i];
}
is also wrong. The type of elements of the array is const char * not int.