"expected class-name before '{' token" when compiling - c++

I've looked a lot, tried a bunch of things I found on stackoverflow and other websites but I still can't figure out how to fix this...
Main C++ file : source.cpp
#include "sources.h"
#include "fft_windows.h"
...
source.h
#include "Array2D.h"
...
Array2D.h
#ifndef ARRAY2D_H_
#define ARRAY2D_H_
#include <cassert>
#include "Features.h"
template <class T> class Array2D{
...
};
#endif
Features.h
#ifndef FEATURES_H_
#define FEATURES_H_
#include <string>
using namespace std ;
class Features : public Array2D {
...
};
#endif
fft_window.h (function declarations that are defined in fft_window.cpp)
//#include "Array2D.h"
template <class T>class Array2D;
void random_example(unsigned int i, Array2D <double> &arr);
...
I've tried everything I found and for some reason I still get this error in Features.h on the class Features : public Array2D { line...
Any idea ?

Array2D is not a class. Array2D<int> is, or Array2D<float>

First, Features.h needs to include Array2D.h, second you get an infinite include recursion with that, and third you need to specify the template parameter when inheriting from Array2D:
class Features : public Array2D<int> {
// example --- ^^^

Array2D is declared in Array2D.h file and you are using that template class in Features.h without #include"Array2D.h". Include that file as said and also remove the inclusion of Feature.h to make sure that there is no recursive includes.
Also, Array2D should be used with template argument, like <int>, <char>, <T> and so on.

Related

Using Template Class in my Dll

following situation:
I have in one dll a template class Point
namespace Image
{
template<typename T> class Point
{
.
.
.
and tring to use this class in another dll. The class looks like:
//Base.h
template<typename T> class Point;
class Base{
Point<double> _Point;
};
//Child.h
#include "Base.h"
class Child : public Base{
Child(Point<double> pt);
doSth();
}
//Child.cpp
#include "Child.h"
#include "Point.h"
Child::Child(Point<double> pt){
_Point = pt;
}
Child::dosth(){
Point<double> p = _Point; // In this Row i get an "undefined type 'Point<double>' Error
}
Any ideas why i get the error?
is my idea totally wrong to forward declare the Point-Class in the header file and make the include in the .cpp ?
Thank you very much, have a nice day!
With the forward declaration you have in Base.h it doesn't matter if you used using namespace Image; first, the template class you declare in Base.h is still in the global namespace and not in the Image namespace. And that declaration will take precedence over the one in the Image namespace.
So there are really two solutions here: Either explicitly use Image::Point, or remove the forward declaration in Base.h (and include the header file where Image::Point<> is defined).

error C2011: 'class type redefinition - Basic Inheritance

Below are by 4 classes, I'm learning about basic c++ syntax and boy is it much harder and less forgiving than other languages I have used. I have a main class, base class "BaseArray" and two sub classes "OrderedArray" and "UnorderedArray".
Main.cpp
#include <iostream>
#include "OrderedArray.cpp"
#include "UnorderedArray.cpp"
using namespace std;
int main() {
system("PAUSE");
return 0;
}
BaseArray.cpp
#include <iostream>
using namespace std;
class BaseArray {
public:
BaseArray::BaseArray() {
}
};
OrderedArray.cpp
#include <iostream>
#include "BaseArray.cpp"
using namespace std;
class OrderedArray : public BaseArray {
OrderedArray::OrderedArray() {
}
};
UnorderedArray.cpp
#include <iostream>
#include "BaseArray.cpp"
using namespace std;
class UnorderedArray : public BaseArray {
UnorderedArray::UnorderedArray() {
}
};
The errors I receive are as followed, from scouting other threads online. I think it might have to do with duplicate calling of classes. To be honest, I have no clue. If someone could point me in the right direction that would be nice, thanks in advance!
error C2011: 'BaseArray':'class' type redefinition
error C2504: 'BaseArray':base class undefined
To fix this error I can remove one of the includes at the top of main.cpp, but I need those to create objects and call functions from the subclasses later on.
You should put your base array in a header:
BaseArray.h
#ifndef BASEARRAY_H_GUARD // include guard
#define BASEARRAY_H_GUARD // include guard
// no using namespace !!
// only includes needed for what's in the header
class BaseArray {
public:
BaseArray();
};
#endif // include guard
And then leave in the cpp only the implementation part of your class:
BaseArray.cpp
#include <iostream>
#include "BaseArray.h"
using namespace std;
BaseArray::BaseArray() { // no need class enclosing: the BaseArray:: prefix is sufficient
}
The you can apply the same principle to the derived classes:
OrderedArray.h
#ifndef BASEARRAY_H_GUARD // include guard
#define BASEARRAY_H_GUARD // include guard
#include "BaseArray.h" // include only those that you need but all those that you need
class OrderedArray : public BaseArray { // requires BaseArray
OrderedArray();
};
#endif
OrderedArray.cpp
#include <iostream> // include headers that are needed for class implementation
#include "OrderedArray.h" // this should be self contained and provide
// evertyhing that is needed for the class itself
using namespace std;
OrderedArray::OrderedArray() {
}
You then have to do the same for UnorderedArray and finally, you have to adapt your main.cpp to include .h instead of .cpp. And you're done.
A final remark: your cpp source code files are now ready for separate compilation. This means that you can no longer compile only main.cpp, hoping that it includes all the code: you have now to compile the 4 cpp files and link them together.

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

Initializing a std::vector in a template class

I get linker errors when compiling the following code:
Here the header file:
// Solver.h
#ifndef SOLVER_H_
#define SOLVER_H_
#include <vector>
#include "Resource.h"
#include "ValueFunction.h"
template<typename T>
class Solver {
public:
Solver(std::vector<Resource>& resources);
private:
std::vector<T> valfuncs;
};
#endif /* SOLVER_H_ */
And here the source file:
// Solver.cpp
#include "Solver.h"
template<typename T>
Solver<T>::Solver(std::vector<Resource>& resources) :
valfuncs(resources.size()) {}
// Explicit class declaration
template class Solver<ValueFunction>;
And the call:
// openadp.cpp
#include "Solver.h"
int main(int argc, char *argv[]) {
std::vector<Resource> resources(4);
Solver<ValueFunction> sol(resources);
return 0;
}
The code is compiling fine if I remove valfuncs(resources.size()) from the initialization list. Why is it not possible to initialize the vector with the class passed from my template list?
Thanks in advance,
Reza
Update
Sorry, but this mini-example does not reproduce the error!
I'm trying to find one which does.
Update 2
The linker error was due to a wrong order of includes in my cmake files.
Remark
This question is not a duplicate of Why can templates only be implemented in the header file? first, because (the most obvious) the code compiles and second, there is an implicite instantiation of the Solver template: template class Solver<ValueFunction>;, thus the compiler is aware of an instance of the defined type.

C++ template syntax

I don't understand what is wrong with this code.
gcc reports "Client.h:29: error: expected template-name before '<' token"
As far as I'm aware I'm followed the template syntax correctly, but it could be that the error message is confusing me and is not the problem
client.h
class Client : public BaseDll<DllClient> [line 29]
{
..snip..
};
basedll.h
template<typename T>
class BaseDll : public Base
{
public:
..snip..
private:
T* _dll;
};
class Base{
};
template<typename T>
class BaseDll:public Base{
public:
private:
T* _dll;
};
class DllClient{
};
class Clien:public BaseDll<DllClient>
{
};
this compiled for me without problems, so I don't think the problem lies within what you posted. My best bet would be that you made a syntax error in client.h, maybe something as simple as forgetting a semicolon after another class definition or some macro that's messing with your code
I'm so sorry everyone, a school-boy error has been made, BaseDll is declared in another namespace. As soon as I added the namespace qualifier, the problem has gone.
Maybe it's just an easy problem: Have you included basedll.h in client.h?
to cover the basics:
does client-h #include basedll.h? do they user different include guards?
Further troubleshooting:
does it work with a non-template base class?
does it work then you typedef the template instaltiation:
typedef BaseDll<DllClient> tClientBase;
class Client : public tClientBase { ... }
[edit] OK, next:
if you put the following two lines directly under the BaseDll declaration:
template <typename T>
class BaseDll
{ ...
};
class DummyFoo;
typedef BaseDll<DummyFoo> tDummyFoo;
I think you snipped away the problem. Are you including something to define 'DllClient' ?
A possible cause of this is that there is an inter-dependency between the different header files:
// client.h
#ifndef CLIENT
#define CLIENT
#include "base.h"
// ...
class Client : public BaseDll<DllClient>
{
// ..snip..
};
#endif
// base.h
#ifndef BASE
#define BASE
#include "client.h"
template<typename T>
class BaseDll : public Base
{
public:
// ..snip..
private:
T* _dll;
};
#endif
Now imagine we're parsing 'base.cpp' then the preprocessor will do the following:
#include "base.h"
#ifndef BASE <--- BASE unset, keep going
#define BASE
#include "client.h"
#ifndef CLIENT
#define CLIENT
#include "base.h"
#ifndef BASE <--- BASE set, skip base.h, return to client.h
class client
: public BaseDll<DllClient> <-- ERROR, BaseDll not defined.
If this is the problem, then you potentially can get around it by forward declaring the base template in client.h:
// client.h
#ifndef CLIENT
#define CLIENT
// #include "base.h" <-- remove include
template <typename DLL_CLIENT>
class BaseDll;
// ...
class Client : public BaseDll<DllClient>
{
// ..snip..
};
#endif