Child class constructor - expected a '{' - c++

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);

Related

Error Implementing Derived Class Constructor: "No Instance of Overloaded Function Matches the Specified Type

The following source aims to create an abstract base class (SubsystemClass) and a derived final class (DisplaySubsystemClass). Implementation of the constructor for the derived class fails on error "no instance of overloaded function "DisplaySubsystemClass::DisplaySubsystemClass" matches the specified type". I am baffled.
SubsystemClass.hpp
#ifndef SUBSYSTEMCLASS_HPP
#define SUBSYSTEMCLASS_HPP
#include <memory>
#include "DriverClass.hpp"
class SubsystemClass
{
protected:
std::shared_ptr<DriverClass> _driver;
public:
virtual ~SubsystemClass();
enum DriverCatalog;
};
#endif
DisplaySubsystemClass.hpp
#ifndef DISPLAYSUBSYSTEMCLASS_HPP
#define DISPLAYSUBSYSTEMCLASS_HPP
#include <memory>
#include "../SubsystemClass.hpp"
#include "DisplayDriverClass.hpp"
class DisplaySubsystemClass final : public SubsystemClass
{
private:
std::shared_ptr<DisplayDriverClass> _driver;
public:
DisplaySubsystemClass(DisplaySubsystemClass::DriverCatalog driverCatalogItem);
~DisplaySubsystemClass();
enum DriverCatalog {
DISPLAY_DRIVER_CONSOLE,
DISPLAY_DRIVER_CURSES,
DISPLAY_DRIVER_SFML,
DISPLAY_DRIVER_OPENGL
};
};
#endif
DisplaySubsystemClass.cpp
#include <memory>
#include "DisplaySubsystemClass.hpp"
#include "SFMLDisplayDriverClass.hpp"
DisplaySubsystemClass::DisplaySubsystemClass(DisplaySubsystemClass::DriverCatalog driverCatalogItem)
{
}
DisplaySubsystemClass::~DisplaySubsystemClass()
{
}
The enumeration should be declared before its using as a parameter type in the constructor.

circular class dependency within template member

#ifndef CLASSB
#define CLASSB
#include "ClassA.h"
namespace name {
class ClassB
{
public:
static Handle conn();
};
}
#endif
-
#include "ClassB.h"
Handle name::ClassB::conn()
{
return getHandle(ClassA::it().str());
}
-
#ifndef CLASSA
#define CLASSA
#include "ClassB.h"
namespace name {
class ClassA
{
public:
template <typename T>
T myFunc(const std::string&)
{
auto tmp = ClassB::conn();
}
};
}
#endif
Calling ClassB::conn() gives a compiler error which says that the class ClassB is not declared. When I forward declare it I get an error message about an incomplete type.
I can't move the template function to my .cpp files as it is a template function. So, how to fix this?
Just remove #include "ClassA.h" from class B's header and it should work. But there appear to be multiple compilation problems with your code so it's hard to say (missing function getHandle, missing it(), missing type Handle etc).

how to declare and use another class's object within a class in c++

I a beginner in programming.
I coded two classes(having constructors with requirement to pass arguments) and want to declare and use one class's object in another class.
I have tried to find the solution to my error on many website, but none of them worked. I also saw a solution to this problem using the 'new' syntax.
Please suggest some(any) way to sought out this problem.
A short program similar the one in which I am facing problems is as follows:
(I know this program is stupid but, this is not actual program I am facing problem in. Instead this is a narrowed down version of the part of the program in which I am facing error)
The error is in Class2.h and main.cpp
main.cpp
#include <iostream>
#include "Class2.h"
using namespace std;
int main()
{
Class2 Class2_Obj;
Class2_Obj.Class2_Function(); // error: undefined reference to `Class2::Class2_Function
return 0;
}
Class1.h
#ifndef CLASS1_H_INCLUDED
#define CLASS1_H_INCLUDED
class Class1
{
private:
const int c1_Variable;
public:
Class1(int);
// Displays the value of c1_Variable on output screan
void Class1_Function();
};
#endif // CLASS1_H_INCLUDED
Class1.cpp
#include <iostream>
#include "Class1.h"
Class1::Class1(int receivedInt) : c1_Variable(receivedInt) {}
void Class1::Class1_Function()
{
cout << c1_Variable;
}
Class2.h
#ifndef CLASS2_H_INCLUDED
#define CLASS2_H_INCLUDED
#include"Class1.h"
class Class2
{
private:
Class1 Class1_Obj(4); // 4 is just a random number.
//error: expected identifier before numeric constant
//error: expected ',' or '...' before numeric constant
public:
// Calls Class1_Function()
void Class2_Function();
};
#endif // CLASS2_H_INCLUDED
Class2.cpp
#include <iostream>
#include "Class1.h"
#include "Class2.h"
void Class::Class2_Function()
{
Class1_Obj.Class1_Function();
}
Here are the links to snapshots of the errors:
Screenshot of Error in Class2.h - http://i.stack.imgur.com/WpK9k.jpg
Screenshot of Error in main.cpp - http://i.stack.imgur.com/yDBD7.jpg
Please help me out! Thanks in advance for any responses :)
The issue is that this in-place initialization of non-static data members syntax is invalid:
class Class2
{
private:
Class1 Class1_Obj(4);
....
};
You can use {} instead,
class Class2
{
private:
Class1 Class1_Obj{4};
....
};
or this form
class Class2
{
private:
Class1 Class1_Obj = Class1(4);
....
};
C++ is a Object Oriented Language. It has classes to structure its data.
To put one class into another, you make an object of one class a member of another class.
Syntactically, it works like
class A {
int x;
public:
A (int x1) : x(x1) {}
};
class B {
A a; // this is how you do it ..
public:
B() : A(4) {}
};
B b; // b is an object which has a member b.a
As you can see, b is an object of class B. It has a member a of class A.

inheritance of exception classes in c++

I'm writing some exception classes in c++ that inherit from a base class and I can't figure out why it won't compile. Any help would be appreciated.
Base Class:
RandomAccessFileException.h
#ifndef RANDOMACCESSFILEEXCEPTION_H
#define RANDOMACCESSFILEEXCEPTION_H
class RandomAcessFileException
{
public:
RandomAcessFileException();
virtual const char* getMessage() = 0;
protected:
char m_message[100];
};
#endif
Derived Class:
RandomAccessFileNotFoundException.h
#ifndef RANDOMACCESSFILENOTFOUNDEXCEPTION_H
#define RANDOMACCESSFILENOTFOUNDEXCEPTION_H
#include "RandomAccessFileException.h"
class RandomAccessFileNotFoundException : public RandomAccessFileException
{
public:
RandomAccessFileNotFoundException(const char* p_filename);
const char* getMessage();
};
#endif
RandomAccessFileNotFoundException.cpp
#include <cstring>
#include "RandomAccessFileException.h"
#include "RandomAccessFileNotFoundException.h"
RandomAccessFileNotFoundException::RandomAccessFileNotFoundException(const char* p_filename)
{
strcat(m_message, "RandomAccessFileNotFoundException: File: ");
strcat(m_message, p_filename);
}
const char* RandomAccessFileNotFoundException::getMessage()
{
return m_message;
}
g++ says:
In file included from RandomAccessFileNotFoundException.cpp:4:0:
RandomAccessFileNotFoundException.h:13:1: error: expected class-name before ‘{’ token
RandomAccessFileNotFoundException.cpp: In constructor ‘RandomAccessFileNotFoundException::RandomAccessFileNotFoundException(const char*)’:
RandomAccessFileNotFoundException.cpp:8:12: error: ‘m_message’ was not declared in this scope
RandomAccessFileNotFoundException.cpp: In member function ‘const char* RandomAccessFileNotFoundException::getMessage()’:
RandomAccessFileNotFoundException.cpp:14:12: error: ‘m_message’ was not declared in this scope
First problem:
You have to:
#include "RandomAccessFileException.h"
In your RandomAccessFileNotFoundException.h header file, since it contains the definition of the base class of RandomAccessFileNotFoundException (i.e. RandomAccessFileException).
So to sum it up, your header file RandomAccessFileNotFoundException.h header should be:
#ifndef RANDOMACCESSFILENOTFOUNDEXCEPTION_H
#define RANDOMACCESSFILENOTFOUNDEXCEPTION_H
#include "RandomAccessFileException.h"
class RandomAccessFileNotFoundException : public RandomAccessFileException
// ^^^^^^^^^^^^^^^^^^^^^^^^^
// This class is defined in the
// RandomAccessFileException.h
// header, so you have to #include
// that header before using this
// class as a base class.
{
public:
RandomAccessFileNotFoundException(const char* p_filename);
const char* getMessage();
};
#endif
Second problem:
Also notice that you have a typo. Your base class is called:
RandomAcessFileException
// ^
Instead of:
RandomAccessFileException
// ^^
Which is the name you are using in RandomAccessFileException.h.
Third problem:
Finally, you are missing a definition of the base class's (RandomAccessFile) constructor, for which you have provided just a declaration:
class RandomAcessFileException
{
public:
RandomAcessFileException();
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^
// This is a DECLARATION of the constructor, but the definition is missing
virtual const char* getMessage() = 0;
protected:
char m_message[100];
};
Without providing a definition, the linker will emit an unresolved reference error.

expected class-name before '{' token 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;
};