1

It is probably some stupid syntax mistake, i have the following class written in h file

#include "IGenticSolverHelper.h" template <class T> class GenericGeneticSolver { public: GenericGeneticSolver(IGenticSolverHelper<T>& helper, int generationSize) : mSolverHelper(helper) { mSolverHelper.GenerateFirstGeneration(0, generationSize, currentGeneration); } private : vector<T> currentGeneration; IGenticSolverHelper<T>& mSolverHelper; }; 

And then the following code :

#include "IGenticSolverHelper.h" #include "GenericGeneticSolver.h" class HelperImpl : IGenticSolverHelper<int> { public: void GenerateFirstGeneration(const int seed,const int generationSize, vector<int>& firstGeneration) { } void Crossover(const int& father,const int& mother, int& son) { } void Mutate(const int& orignal, int& mutated) { } float Cost(int& solution) { } }; int main() { int a =5; GenericGeneticSolver<int> mySolver(HelperImpl,a); } 

And i get the following error when i compile :

error C2061: syntax error : identifier 'a' 

if i will change the line to :

GenericGeneticSolver<int> mySolver(HelperImpl); 

it will compile, though the constructor expect 2 arguments, and will get the following warning :

warning C4930: 'GenericGeneticSolver<T> mySolver(HelperImpl)': prototyped function not called (was a variable definition intended?) 

And to add to the oddness, when i put a break point on this line, he won't stop there.

What am i doing wrong, i just trying to create an instance of GenericGeneticSolver

1 Answer 1

3

Take a look at this line:

GenericGeneticSolver<int> mySolver(HelperImpl,a); 

The compiler is confused about what you're trying to do here because HelperImpl is the name of a type, while a is the name of an object. The compiler thinks what you're doing is trying to prototype a function named mySolver that takes in a parameter of type HelperImpl and a parameter of type a, but then gets stuck because it doesn't know of any types named a.

If you remove a, you get this:

GenericGeneticSolver<int> mySolver(HelperImpl); 

This is a perfectly legal prototype of a function called mySolver that takes an argument of type HelperImpl and returns a GenericGeneticSolver<int>. The warning you're getting is the compiler telling you that you might not have meant to make this a prototype, since it somewhat looks like an instantiation of a variable named mySolver but isn't.

Since I assume that you're trying to instantiate an object of type GenericGeneticSolver<int> here, you probably want to instantiate a HelperImpl and then pass that object into the constructor, like this:

HelperImpl hi; GenericGeneticSolver<int> mySolver(hi, a); 

Hope this helps!

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, i didn't noticed that i pass a type and not an instance !
We call them declarations in C++, not "prototypes". GCC's C++ front-end is senile in this regard.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.