Two errors using class and header - c++

I get the error class redefinition
Also the error ID is not a member of class process
and missing semi colon before ID
I tried char* instead of string.. not working also
Any help? I seem to be missing something
Thanks in advance
process.h file
#ifndef PROCESS_H
#define PROCESS_H
class process
{
public:
string ID;
int run_time;
int arrival_time;
process();
int get_start_time();
int get_finish_time(int start, int run);
int get_TA_time(int finish, int start);
float get_weighted_TA_time();
};
#endif
process.cpp file
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <sstream>
#include "process.h"
using namespace std;
class process
{
public:
process:: process() {};
int process:: get_start_time()
{
int x;
return x;
}
int process:: get_finish_time(int start, int run)
{
int x;
return x= start +run;
}
int process:: get_TA_time(int finish, int start)
{
int x;
return x= finish - start;
}
float process:: get_weighted_TA_time()
{};
};

Problem 1 Class refinition
In cpp file you don't need to write class process. It should be
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <sstream>
#include "process.h"
using namespace std;
process:: process() {};
int process:: get_start_time()
{
int x;
return x;
}
int process:: get_finish_time(int start, int run)
{
int x;
return x= start +run;
}
int process:: get_TA_time(int finish, int start)
{
int x;
return x= finish - start;
}
float process:: get_weighted_TA_time()
{};
In header file you have given required declarations. In cpp, you need to give the definitions. But because you are outside the class scope now, you must give the fully qualified name of members which you are already doing. (for ex: int process:: get_finish_time(int start, int run))
If you again use class process, compiler has no hint that you intend the same class and not want a new class causing the class redifinition error. In C++, it is not allowed to make a class partially and complete the rest of it somewhere else. (Not to be cofused with inheritance)
Problem 2: string issue
Add #include <string> in your header file and use std::string or add line using std::string

There are a few issues. One is this:
process.h needs to #include <string>. Once included, prepend all string variables with std::.
#ifndef PROCESS_H
#define PROCESS_H
#include <string>
class process
{
public:
std::string ID;
int run_time;
int arrival_time;
process();
int get_start_time();
int get_finish_time(int start, int run);
int get_TA_time(int finish, int start);
float get_weighted_TA_time();
};
#endif
The problem with your original code is that you're:
Relying that some outside module included <string> before process.h is included.
Relying that some outside module stated using namespace std; before including process.h.
None of those can be guaranteed.

Related

string return function not working - 'identifier is underfined'

Relearning C/C++ after 3 years of JavaScript (I've gotten way too comfortable..)
I'm building a test file with input.
The problem is within cTool, where the first function is not letting me return a string. I thought this was totally valid if the library is included in the header file? What am I overlooking here.
cTool.cpp
string getInfo(void) {
}
void parseInfo(void (*getInfo)()) {
}
float assessInfo(float number) {
}
...
cTool.h
#pragma once
#ifndef ASSESS_GRADE_H
#define ASSESS_GRADE_H
#include <string>
#include <stdio.h>
#include <iostream>
using namespace std;
string getInfo(void);
void parseInfo(void(*getInputFunc)());
float assessInfo(float number);
float assessInfo(char letter);
float assessInfo(int *array);
#endif
cMain.cpp
#include "cTool.h";
int main (void) {
// function call from cTool.cpp
return 0;
}
You need to add #include "cTool.h" to cTool.cpp, not just to cMain.cpp only. Otherwise, when compiling cTool.cpp, the compiler doesn't know what a string is since it doesn't see your #include <string> and using namespace std; statements (BTW, using namespace std; in a header file is a very bad idea).
cTool.cpp
#include "cTool.h" // <-- ADD THIS!
std::string getInfo(void) {
}
void parseInfo(void (*getInfo)()) {
}
float assessInfo(float number) {
}
...
cTool.h
#pragma once
#ifndef ASSESS_GRADE_H
#define ASSESS_GRADE_H
#include <string>
#include <iostream>
std::string getInfo(void);
void parseInfo(void(*getInputFunc)());
float assessInfo(float number);
float assessInfo(char letter);
float assessInfo(int *array);
#endif
cMain.cpp
#include "cTool.h";
int main (void) {
// function call from cTool.cpp
return 0;
}

Debugging C++ compiler error

I'm still a noobie in c++ so I am not to skilled in debugging yet. Just trying to figure out how to fix this compilation error.
CruiseShip.cpp:11: error: expected ā€˜)ā€™ before ā€˜nā€™
CruiseShip.cpp
#include "CruiseShip.h"
#include "Ship.h"
#include <iostream>
using namespace std;
Ship s;
int passengers;
CruiseShip(string n, string y, int p) : Ship(n,y)
{
passengers=p;
}
void print()
{
cout<<"Name: "<<s.getName()<<"\nMaximum passengers:"<<passengers<<endl;
cout<<"-------------------------"<<endl;
}
CruiseShip.h
#ifndef CRUISESHIP_H
#define CRUISESHIP_H
#include "Ship.h"
#include <string>
using namespace std;
//class Ship;
class CruiseShip:public Ship{
private:
int passengers;
Ship::Ship s;
public:
CruiseShip(string, string, int);
virtual void print();
};
#endif
Ship.h
#ifndef SHIP_H
#define SHIP_H
#include <string>
using namespace std;
class Ship{
private:
string name;
string built;
public:
Ship();
Ship(string, string);
string getName();
string getBuilt();
virtual void print();
};
#endif
You have 3 errors:
1 and 2. You don't declare print and CruiseShip (The constructor) as part of the class CruiseShip when you define them. You need to:
CruiseShip::CruiseShip(string n, string y, int p) : Ship(n,y) {
virtual void CruiseShip::print() {
3, you dont have a namespace Ship so this is unnecessary:
Ship::Ship s; // This only needs to be Ship s <- NameSpace::ObjectType nameOfObject;
After this it will compile http://ideone.com/wJ6mPO. It will not link however, because you have undefined references to all of the functions you have yet to define.

What's wrong - expected class-name before '{' token

What's wrong in this code ? I get [Error] expected class-name before '{' token (Pralka.h line 14 )
i know there are lots of similar questions here. I've googled too but I can't get over it. So I would like to show you my code..
I wrote this very simple code to train myself on inheritance and virtual functions..
main.cpp:
#include <iostream>
#include <fstream>
#include <string>
#include "AGD.h"
using namespace std;
int main() {
Pralka p1("polar", 1250);
AGD *A;
A = &p1;
}
AGD.h:
#ifndef AGD_H
#define AGD_H
#include <iostream>
#include "Pralka.h"
class AGD {
private:
static int liczba_sprzetow;
public:
AGD(){
liczba_sprzetow++;
}
~AGD(){
liczba_sprzetow--;
}
static int get_liczba_sprzetow() {
return liczba_sprzetow;
}
virtual double get_cena() {
}
};
#endif
Pralka.h:
#ifndef PRALKA_H
#define PRALKA_H
#include <iostream>
#include <string>
using namespace std;
class Pralka : public AGD
{
private:
string marka;
double cena;
public:
Pralka(string m, double c): marka(m), cena(c){
}
Pralka(){
}
~Pralka(){
}
string get_marka() const{
return marka;
}
double get_cena() const{
return cena;
}
Pralka& operator=(const Pralka& Q){
marka=Q.marka;
cena=Q.cena;
}
};
#endif
I get also [Error] cannot convert 'Pralka*' to 'AGD*' in assignment but why? I don't understand (main.cpp line 29).
AGD.h is including Pralka.h, but it should be the other way round (Pralka.h should be including AGD.h).
The reason is that Pralka needs to see the AGD declaration to inherit from it. AGD doesn't need to see the Pralka declaration.

Classes as parameters error

In Weapon.h, when I try and take a class 'Entity*' as a parameter, it says "Syntax error: identifier 'Entity'" when I compile. Additionally when I roll over the text 'target', Visual C++ Express 2010 gives me the text " *target". The Entity class is fine and I'm pretty sure it's included correctly.
(I won't post Player.h as it's unnecessary - see Library.h - but it has a header guard and includes Entity.h)
Library.h:
#ifndef _LIBRARY_
#define _LIBRARY_
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdarg>
#include <vector>
#include <ctime>
#include <cmath>
#include <cstdlib>
#include <map>
#include <exception>
#include <sstream>
//file includes
#include "Globals.h"
#include "Player.h"
#include "Exception.h"
#include "Weapon.h"
#include "Armour.h"
#include "Consumable.h"
//prototypes that require "Library.h"
bool Poglathon(std::vector<std::string>& text,Player *player);
bool PoglathonTown(std::vector<std::string>& text,Player *player);
std::map<std::string,Weapon*> init_weapons(void);
std::map<std::string,Armour*> init_armour(void);
std::map<std::string,Consumable*> init_consumables(void);
#endif //__LIBRARY__
Weapon.h:
#ifndef _WEAPON_H_
#define _WEAPON_H_
#include "Shopable.h"
class Weapon : public Shopable{
private:
int Damage;
public:
Weapon(int c,int d,std::string n) : Shopable(c,n),Damage(d){}
std::string getDesc() const{
return getName()+"\t"+tostring(Damage)+"\t"+tostring(Cost);
}
int getDamage() const{return Damage;}
int DamageTarget(Entity* target){
int DamageDealt = 0;
//do damage algorithm things here
return DamageDealt;
}
};
#endif
Shopable.h:
#ifndef _SHOPABLE_H_
#define _SHOPABLE_H_
#include "Library.h"
class Shopable{
protected:
std::string Name;
int Cost;
std::string Description;
public:
Shopable(int c, std::string n):Cost(c),Name(n){}
std::string getName() const{return Name;}
int getCost() const {return Cost;}
virtual std::string getDesc() const = 0;
};
#endif
Entity.h:
#ifndef _ENTITY_
#define _ENTITY_
#include "Library.h"
#include "Weapon.h"
#include "Armour.h"
#include "Consumable.h"
class Entity{
public:
void printStats() const;
void heal(double health);
std::string name;
protected:
//player stats
double str; //strength
double wis; //wisdom
double ref; //reflex
double hp; //health points
double maxHp; //maximum health points
double i; //initiative
double inte; //intelligence
double c; //courage
int gold; //gold
int xp; //experience
int ap; //armour points
int wd; //weapon damage
int lvl; //level
int sp; //skill points
Weapon* weapon;//weapon
Armour* cArmour;//current armour
};
#endif
In C++, classes must be declared before they are referenced. You are #include-ing Weapon.h in Entity.h, but at that point, the compiler doesn't know about the existence of class Entity.
You will either need to change the order in which things are declared, or add a forward declaration "above" class Weapon. It can simply be:
class Entity;
That tells the compiler that there is such a name as Entity. However, it doesn't tell it anything about what members it has, so you can't actually do anything with it, other than declare variables of Entity * and Entity &, and pass them around.
Your headers include each other because your classes refer to each other. (But your compiler doesn't suffer from a stackoverflow because of your include guards - that's a good thing!)
You should arrange your header files hierarchically, ie there are files at the 'top' which #include nothing and files 'below' which include some of the top ones and so-on down the hierarchy. But at no point should there be 'loops'.
In order to break your loops in your code, any classes that refer to each other should forward declare any mutual dependencies and only refer to dependency names and not their members.
e.g.
Entity.h
class Weapon;
class Entity{
...
Weapon* weapon;
};
Weapon.h
class Entity;
class Weapon{
...
int DamageTarget(Entity* target);
};
Notice how Weapon.h only refers to Entity*.
You will need to define int Weapon::DamageTarget(Entity* target) in Weapon.cpp
#include <entity.h>
Or forward-declare only in the header
class Entity;
This makes compilation a bit faster (you still need to include it to use in the implementation).
Weapon.h doesn't #include Entity.h, or anything that recursively includes it. Therefore, it doesn't know about the class Entity.

C++ Does not name to a type

This might be an easy question, but I cannot figure out why the compiler it's giving me this error. I have two classes. Agent and Environment. WHen I try to add an object of type Agent in my Environment class I get Agent does not name to a type error. I am including Agent.h in my Environment.h class
#ifndef AGENT_H_INCLUDED
#define AGENT_H_INCLUDED
#include <vector>
#include <iostream>
#include "Environment.h"
using namespace std;
class Agent{
public:
Agent(bool s);
vector<int> getPercept();
void setPercept(vector<int> p);
void goForward();
void turnRight();
void turnLeft();
void clean();
void paint();
void refuel();
bool needsRefuel();
void turnOn();
void turnOff();
bool isActive();
void move();
int getCurX();
int getCurY();
char getCurDir();
void setCurrentPosition(int x, int y, char d);
private:
vector<int> percept;
int actions;
int performance;
char direction;
bool isOn;
int curX;
int curY;
char curDir;
};
#endif // AGENT_H_INCLUDED
/*************************/
#ifndef ENVIRONMENT_H_INCLUDED
#define ENVIRONMENT_H_INCLUDED
#include <vector>
#include <iostream>
#include "Agent.h"
using namespace std;
class Environment{
public:
Environment(vector<vector<char> > roomData);
Environment(vector<vector<char> > roomData, vector<int> status);
void setRoomData(vector<vector<char> > roomData);
bool isSimulationComplete();
void isAgentHome();
vector<int> sendLocationStatus();
void printEnvironment();
void setAgentHome(int x, int y);
vector<int> getAgentPercept();
void setAgentPercept(vector<int> status);
void setAgentPosition(int x, int y, char p);
vector<int> sendAgentPercept();
void calculateAgentPercept();
private:
vector<vector<char> > room;
vector<int> agentPercept;
bool simulationComplete;
int agentHomeX;
int agentHomeY;
int agentX;
int agentY;
char agentDir;
Agent agent; ////ERROR IS HERE
};
#endif // ENVIRONMENT_H_INCLUDED
Your agent.h includes environment.h. The agent.h file is parsed in order from top to bottom, so when environment.h is parsed, the compiler doesn't know what an Agent is. There appears to be no reason to incude environment.h in agent.h.
Apart from what the comments already said, you can't have two header files include each other. There is no reason for Agent.h to include Environment.h, so if a .cpp file includes Agent.h first, it'll fail (since it will first go through Environment.h, which requires Agent).
IF you have a situation where two header files depend on each other's definitions, use forward declarations where you can, or split your header files up into more header files.