expected class-name before '{' token c++ - c++

I've been googling and reading about this and didn't come up with an answer yet, maybe someone can help me with this please.
the error I get is: expected class-name before '{' token
Carte_num.h
#ifndef CARTE_NUM_H
#define CARTE_NUM_H
#include <string.h>
#include <iostream>
#include "Carte.h"
using namespace std;
class Partie;
class Carte_num : public Carte
{ //<--------------this is where I get the error
public:
Carte_num(int haut,string typ, char coul [8], int nb_p);
~Carte_num();
protected:
int hauteur;
public:
friend Partie;
};
#endif // CARTE_NUM_H
Carte.h
#ifndef CARTE_H
#define CARTE_H
#include <iostream>
#include <string.h>
#include "Partie.h"
using namespace std;
class Joueur;
class Partie;
class Carte
{
public:
Carte();
Carte( string typ, char coul [8], int nb_p);
~Carte();
protected:
char couleur[8];
int nb_pts;
string type;
public:
//bool action(Partie p);
string definir();
bool est_valable(Partie p);
//int getnb_pts() { return(nb_pts);}
friend class Joueur;
friend class Partie;
};
#endif // CARTE_H
the error I get is: expected class-name before '{' token where I indicated earilier

First, the friend declaration should be
friend class Partie;
Second, you need to include the <string> header, without the trailing .h. That is where std::string is defined.
Third, you could have a circular include dependency, for example if Partie.h includes Carte.h or Carte_num.h. You can fix that by removing #include "Partie.h" from Carte.h (you may need to include it in Carte's implementation file).
Another possibility is that you have a missing ; after your class Carte declaration in Carte.h.

Your friend declaration is incorrect.
See the correct format:
class Carte_num : public Carte
{
// ...
friend class Partie;
};

Related

Getting 'undeclared identifier' when using vector of type class

Having trouble understanding why I'm getting an 'undeclared identifier' error when I've made sure to include the header file that has the declaration of the class I'm making a vector of.
#pragma once
#include <vector>
#include "Member.h"
class Party {
private:
std::vector<Member> members;
public:
Party();
int get_party_size();
void add_member(Member new_member);
Member& get_member(int num);
};
Here's "Member.h"
#pragma once
#include <vector>
#include <string>
#include "Party.h"
class Member
{
private:
int hp;
bool is_stunned;
bool is_alive;
public:
Member();
~Member();
int get_hp();
bool get_is_stunned();
bool get_is_alive();
void take_damage(int amt);
void stun();
virtual void turn(std::vector<Party>& parties, int my_party, int my_member_number);
virtual std::string get_class_name();
};
Pretty new to the language, so sure I'm missing something obvious.
You have circular dependency between Member and Party
Remove the line
virtual void turn(
std::vector<Party>& parties,
int my_party,
int my_member_number);
in Member and remove the #include "Party.h" in Member.h
Instead think along the lines that a Party is just a collection of Members so there is no need for an individual Member to know about the container
So after input from #some-programmer-dude you could also solve it by adding a forward declaration in your Member.h instead of including the Party.h
class Party;
class Member { ... }

Expected class-name before { token - very simple

I've seen many answers for this question, know what to look for and still can see that. Looks like some obvious problem.
Algorithm.h:
#ifndef ALGORITHM_H
#define ALGORITHM_H
#include <iostream>
using namespace std;
template <typename T>
class Algorithm
{
private:
T data;
T result;
public:
Algorithm(T in){
data = in;
}
void compute();
void displayData(){
cout<<data<<endl;
}
T getResult(){
return result;
}
};
#endif // ALGORITHM_H
Bubble.h:
#ifndef BUBBLE_H
#define BUBBLE_H
#include "algorithm.h"
class Bubble : public Algorithm{
public:
Bubble();
};
#endif // BUBBLE_H
main.cpp
#include "bubble.h"
#include <iostream>
using namespace std;
int main()
{
Algorithm<int> a(1);
Algorithm<char> b('a');
a.displayData();
b.displayData();
return 0;
}
Error is:
/home/user/Projects/Algorithms/main.cpp:1: In file included from
../Algorithms/main.cpp:1:0: /home/user/Projects/Algorithms/bubble.h:6:
error: expected class-name before '{' token class Bubble : public
Algorithm{
^
Why compiler cannot see Algorithm class? I included it in Bubble.h, so?
You forgot to provide the template argument for Algorithm. If you fix this, your code compiles fine. (Live)
Bubble inherits from the Algorithm class, which is a template. So it also needs the template specification:
#ifndef BUBBLE_H
#define BUBBLE_H
#include "algorithm.h"
template <typename T>
class Bubble : public Algorithm<T> {
public:
Bubble();
};
#endif // BUBBLE_H

"error: expected class-name before '{' token" trying to inheritance

I tried to create a queue that inherits from list and get this error:
"error: expected class-name before '{' token"
these are the codes that I have ...
cola_lista.cpp
#ifndef cola_hereda_lista
#define cola_hereda_lista
#include <iostream>
#include "lista_t.hpp"
//#include "nodo_t.hpp"
using namespace std;
template <class T>
class cola : public lista{
private:
nodo<T> *frente, *final;
public:
cola();
bool es_vacia();
int longitud(); //
void encolar(T e);
void desencolar(); //precondicion ¬es_vacia
T obtener_frente(); //precondicion ¬es_vacia
~cola();
};
#endif
lista.hpp
#ifndef lista_template
#define lista_template
#include <iostream>
#include "nodo_t.hpp"
using namespace std;
template <class T>
class lista{
private:
nodo<T> *primero, *ultimo;
int cantidad;
public:
//
};
nodo.hpp
#include <iostream>
#ifndef nodo_template
#define nodo_template
using namespace std;
template <class T>
class nodo{
private:
T elemento;
nodo<T> *siguiente;
public:
nodo();
T get_elem();
void set_elem(T e);
nodo<T>* get_siguiente();
void set_siguiente(nodo<T> *sigui);
~nodo();
};
I've been hours trying to figure out what is what is ill-posed in the code. Help!
change your code to this
template <class T>
class cola : public lista<T>{
You need to adjust your declaration of cola:
template <class T>
class cola : public lista<T>
^^^
cola is a class template and you need to specify the type. Also you should not put using namespace std; in your header files and I would discourage you from using it in general, this previous thread Why is 'using namespace std;' considered a bad practice in C++? goes into why.

Child class constructor - expected a '{'

I'm still learning C++ and some of the people here helped me a lot, thank you all.
I have another problem now : I have a class B derived from class A like this :
ClassB.h
#ifndef CLASSB
#define CLASSB
#include <cstdlib>
#include <string>
#include <vector>
#include <time.h>
using namespace std;
#include "ClassA.h"
class ClassA;
class ClassB: public ClassA{
public:
ClassB(ClassC* classCinstance, int gnr, int type) : ClassA(classCinstance);
};
#endif
ClassB.cpp
#include "ClassB.h"
ClassB::ClassB(ClassC* classCinstance, int gnr, int type) : ClassA(classCinstance){
//Some stuff
}
The problem is that when I compile, it says that :
error C2969: syntax error : ';' : expected member function definition
to end with '}'
And Visual Express tells me :
Error: expected a '{'
when I point my cursor to the semicolon finishing line 18 in ClassB.h (the declaration of constructor of ClassB).
How can I solve that ? I declared this constructor so it exists... And I declare its body in the .cpp so... Everything's fine, right ?
ClassB constructor is bad declared:
ClassB(ClassC* classCinstance, int gnr, int type) : ClassA(classCinstance);
must be
ClassB(ClassC* classCinstance, int gnr, int type);
The declaration of the constructor for ClassB in the header has a colon list, which it shouldn't. That's part of the definition.
So:
ClassB(ClassC* classCinstance, int gnr, int type) : ClassA(classCinstance);
should read:
ClassB(ClassC* classCinstance, int gnr, int type);

Is this possible in C++? toString(ClassName* class)

I'd like to use toString with class argument, but for some reason there is an error.
The code is:
Animal.h
#include "Treatment.h"
#include "jdate.h"
#include <vector>
class Animal{
protected:
int id;
double weight;
int yy;
int mm;
int dd;
double accDose;
char sex;
vector<Treatment*> treatArray;
public:
Animal();
Animal(int newid, double newweight, int yy, int mm, int dd, char newsex, vector<Treatment*> treatArray);
~Animal();
};
Treatment.h
#ifndef TRE_H
#define TRE_H
#include <string>
#include <sstream>
#include "jdate.h"
#include "Animal.h"
#include "Cattle.h"
#include "Sheep.h"
class Treatment{
private:
int id;
jdate dayTreated;
double dose;
public:
Treatment(int id,jdate dayTreated, double dose);
Treatment();
~Treatment();
string toString(Animal* a);
};
#endif
Treatment.cpp
#include "Treatment.h"
using namespace std;
Treatment::Treatment(int newid,jdate newdayTreated, double newdose){
id=newid;
dayTreated = newdayTreated;
dose = newdose;
}
Treatment::Treatment(){
id=0;
dose=0;
}
Treatment::~Treatment(){}
string Treatment::toString(Animal* a)
{
string aa;
return aa;
}
toString is in Treatment class. I'm not sure but I think it's because Animal has
vector treatArray;. Does it actually matter?
Sorry that I cannot put the error messages here, because once I declare toString, for some reason tons of errors occur, such as
Error 1 error C2065: 'Treatment' : undeclared identifier l:\2011-08\c++\assignment\drug management\drug management\animal.h 30 1 Drug Management
// Animal.h
// #include "Treatment.h" remove this
class Treatmemt; // forward declaration
class Animal
{
...
};
In your version, Treatment.h and Animal.h include each other. You need to resolve this circular dependency using forward declaration. In .cpp files, include all necessary h-files.
You include Animal.h in Treatment.h before the class Treatment is defined, that's why you're getting the error.
Use forward declaration to solve this:
In Animal.h add line
class Treatment;
You've not used std namespace in both header files.
So, use std::string instead of string. Because string is defined in std namespace.
Simillarly use std::vector instead of vector.
There are a few problems with your code. First of all, Animal.h should have an include guard as Treatment.h has:
#ifndef ANIMAL_H
#define ANIMAL_H
// Your code here
#endif
I'd also suggest that you give a longer name to Treatment.h's guard, you'll reduce the risk of using the same name elsewhere.
Using guards will ensure that you don't have a circular inclusion. Still you do have a circular dependency because the Treatment class needs to know about the Animal class and vice versa. However, as in both cases you used pointers to the other class, the full definitions are not required and you can - should, actually - replace in both header files the inclusion of the other with a simple declaration. For instance in Animal.h remove
#include "Treatment.h"
and add
class Treatment;
Both Treatment.cpp and Animal.cpp must include both Treatment.h and Animal.h.