C++ syntax help, using templates - c++

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

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!

Related

"Expected a type specifier" error when creating an object of a class inside another class declaration

I have a class called scratch and have used scratch.h to declare it.
Now I have another class called scratch2 under scratch2.h and want to create an object of scratch as a shared pointer.
This is the syntax I used inside scratch2 class declartion:
std::shared_ptr<scratch> newObject(new scratch());
But I am getting this error: Error: Expected type specifier
So I tried this instead:
std::shared_ptr<scratch> newObject2 = std::make_shared<scratch>();
which works fine. Can anyone please tell me why the first one isn't working?
My scratch.h code:
#ifndef _SCRATCH_
#define _SCRATCH_
#include <iostream>
class scratch {
private:
int _a;
float _b;
std::string _s;
public:
scratch();
scratch(int a, float b, std::string n);
~scratch();
};
#endif
and my scratch2.h:
#ifndef _SCRATCH_2_
#define _SCRATCH_2_
#include "scratch.h"
#include <memory>
class scratch2 {
std::shared_ptr<scratch> newObject(new scratch()); // Expected a type specifier error occurs here
std::shared_ptr<scratch> newObject2 = std::make_shared<scratch>(); // works fine here
};
#endif
Because in the context of declaring class members:
std::shared_ptr<scratch> newObject(new scratch());
This initially looks to the compiler as a class method declaration. C++'s syntax is very complicated. You can look at the entire declaration and understand what it's trying to do, but the compiler is parsing keywords one keyword at a time, and sees this:
type name( ...
inside a class declaration, and this starts to look like a class method declaration, and that's what the compiler tried to parse, and failed.
The formal specification of the C++ language spills a lot of ink on the subject of how things should be declared, mindful of the current state of compiler technology.
You need to work with the compiler, and use an alternate syntax that's unambiguous:
std::shared_ptr<scratch> newObject = std::shared_ptr<scratch>(new scratch());
Verified with gcc 5.3
Inside of a class definition, there are only two ways you're allowed to initialize your members. You can use = and you can use {}. You are not allowed to use ():
struct foo {
int x = 4; // OK
int y{7}; // OK
int z(12); // error
};
Admittedly, the compiler error in this case is extremely unhelpful.

"Incomplete type not allowed " when creating std::ofstream objects

Visual Studio Throws this Strange Error:
Incomplete type not allowed
When I try to create an std::ofstream object. Here is the code I wrote inside a function.
void OutPutLog()
{
std::ofstream outFile("Log.txt");
}
whenever it encounters this Visual Studio throws that Error. Why This Happens?
As #Mgetz says, you probably forgot to #include <fstream>.
The reason you didn't get a not declared error and instead this incomplete type not allowed error has to do with what happens when there is a type that has been "forward declared", but not yet fully defined.
Look at this example:
#include <iostream>
struct Foo; // "forward declaration" for a struct type
void OutputFoo(Foo & foo); // another "forward declaration", for a function
void OutputFooPointer(Foo * fooPointer) {
// fooPointer->bar is unknown at this point...
// we can still pass it by reference (not by value)
OutputFoo(*fooPointer);
}
struct Foo { // actual definition of Foo
int bar;
Foo () : bar (10) {}
};
void OutputFoo(Foo & foo) {
// we can mention foo.bar here because it's after the actual definition
std::cout << foo.bar;
}
int main() {
Foo foo; // we can also instantiate after the definition (of course)
OutputFooPointer(&foo);
}
Notice we could not actually instantiate a Foo object or refer its contents until after the real definition. When we only have the forward declaration available, we may only talk about it by pointer or reference.
What is likely happening is you included some iostream header that had forward-declared std::ofstream in a similar way. But the actual definition of std::ofstream is in the <fstream> header.
(Note: In the future be sure to provide a Minimal, Complete, Verifiable Example instead of just one function out of your code. You should supply a complete program that demonstrates the problem. This would have been better, for instance:
#include <iostream>
int main() {
std::ofstream outFile("Log.txt");
}
...also, "Output" is generally seen as one complete word, not two as "OutPut")

Passing Stack to Function

So I'm playing around with stacks and I've filled one in my main function, but now I want to pass it to my other functions so I can traverse through it. I'm not sure what kind of data type to put into the prototype though so that it accepts it. Suggestions? Here's what I have:
Main.cpp
#include <iostream>
using namespace std;
#include "stack.h"
void displayStack(char &stackRef);
int main()
{
Stack<char> stack;
stack.push('a');
stack.push('b');
stack.push('c');
return 0;
};
void displayStack(char starRef)
{
// Cannot Get here - Errors!
};
It's telling me I have too many arguments and it doesn't match argument list.
This should suffice:
void displayStack(const Stack<char>& stack);
The name DisplayStack indicates that the function only displays the stack, not changing it in any way. So then the argument can be a reference to const. However, the suffix Stack in the name is redundant since it is implied by the argument, so I’d do it like this:
#include <iostream>
using namespace std;
#include "stack.h"
typedef Stack< char > CharStack;
void display( CharStack const& stack )
{
// ... Display the stack
}
int main()
{
CharStack stack;
for( auto const ch : { 'a', 'b', 'c' } )
{
stack.push( ch );
}
display( stack );
}
Note that …
The function has been moved above main. No silly pure declaration required then, less work. DRY: Don't Repeat Yourself.
Incorrect semicolons after the function definitions, have been removed. Well, at least I think they’re incorrect. Whether they are or not, they’re totally superfluous.
Superfluous return 0; in main has been removed, because that is the default. However, some programmers prefer to have it explicit.
On the downside, while the C++11 loop compiles nicely with g++ 4.7.2, it causes an Internal Compiler Error (ICE) with Visual C++ 11.0:
[d:\dev\test]
> cl foo.cpp
foo.cpp
foo.cpp(7) : warning C4100: 'stack' : unreferenced formal parameter
foo.cpp(16) : error C2059: syntax error : '{'
foo.cpp(16) : error C2143: syntax error : missing ';' before '}'
c1xx : fatal error C1063: INTERNAL COMPILER ERROR
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
[d:\dev\test]
> _
Oh well.
Do that your way. ;-)
Compiler bug reported to Microsoft.
If you do not want to modify contents of the stack inside the function:
void displayStack(const Stack<char> &starRef)
If you want to modify the contents of the stack inside the function:
void displayStack(Stack<char> &starRef)
Points to note:
The type of the variable being passed must be the type you mention in function prototype.
In C/C++, by default all arguments to function are passed by copy, i.e: A copy of the argument rather than the argument itself is passed to the function. The overhead is the copy. You pass by reference to avoid overhead of a copy of variable being passed.
You use const qualifier on the argument if you want the passed variable to be immutable in the function.
Change your displayStack function to:
void displayStack(Stack<char> &stackRef)

Calling a member function with member data by using for_each

Dear all, I would like to call a member function (that expects a reference) for each object of (let's say) a vector that is a member of the same class, as the following code shows:
#include <functional>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
struct Stuff {
double x;
};
class Test {
public:
void f1(Stuff & thing);
void f2(void);
vector<Stuff> things;
};
void Test::f1(Stuff & thing) {
; // do nothing
}
void Test::f2(void) {
for_each(things.begin(), things.end(), f1);
}
int main(void)
{
return 0;
}
This codes gives me a compiler error related to unresolved overloaded function type . I have tried also with bind, but it seems that the references requisite in f1 is one problem. I know I am missing something important here, so I take this opportunity to solve my problem and to learn. At the moment, I can't install boost, but I would like to know also if boost is useful to solve this problem. Thanks in advance.
The function you want to call cannot be simply identified by f1 but should be referred to as &Test::f1 (as in : member function f1 of class Test)
Function f1 does not take a single argument : as any non-static member function it has an implicit this parameter of type Test * const
Finally, a standard bind won't be able to do the trick because it doesn't handle parameters passed by reference.
Boost.Bind would indeed be a great option :
std::for_each(things.begin(), things.end(), boost::bind(&Test::f1, this, _1));

Why C++ compiler (gcc) thinks function is `virtual' field?

I have a the following method definition in my class:
virtual Calc* Compile(
Evaluator* evaluator, ResolvedFunCall* fun_call, string* error);
For some reason, GCC complains that:
error: 'Compile' declared as a 'virtual' field
Any ideas why it would believe Compile to be a field, instead of a method?
I get that error when the first parameter doesn't make sense to it. Check that Evaluator is known as type:
struct A {
virtual void* b(nonsense*, string*);
};
=> error: 'b' declared as a 'virtual' field
struct A {
virtual void* b(string*, nonsense*);
};
=> error: 'nonsense' has not been declared
To find out whether something is a object or function declaration, the compiler sometimes has to scan the whole declaration. Any construct within the declaration that could possibly form a declaration is taken to be a declaration. If not, then any such construct is taken to be an expression. GCC apparently thinks because nonsense is no valid type, it can't be a valid parameter declaration, and thus falls back treating the whole declaration as a field (note that it says in addition error: expected ';' before '(' token ) . Same thing in local scope
int main() {
int a;
// "nonsense * a" not treated as declaration
void f(nonsense*a);
}
=> error: variable or field 'f' declared void
int main() {
// "nonsense * a" treated as parameter declaration
typedef int nonsense;
void f(nonsense*a);
}
=> (compiles successfully)
This happened to me when I declared a virtual function with {} instead of ().
A sample to demonstrate the error:
test.h
class Test{
public:
Test(){}
~Test(){}
//next line is defective, use () instead of {}
virtual int myfunct{int i};//notice brackets here which causes the error
};
test.cpp
#include "test.h"//i omitted include guards for simplicity
int Test::myfunct(int x){
x=5;
return x;
}
main.cpp
#include "test.h"
int main(){
Test test;
return 0;
}