Okay, I thought I had implementation files for template classes figured out, but apparently not... I have the following files in a VS 2013 C++ solution:
Main.cpp
#include "StateManager.h"
#include "State.h"
enum class Derp {
Herp,
Lerp,
Sherp,
};
int main() {
Game2D::State<Derp>::Context context(5);
Game2D::StateManager<Derp> mgr(context);
return 0;
}
StateManager.h
#pragma once
#include "State.h"
namespace Game2D {
template<typename Id>
class StateManager {
private:
typename State<Id>::Context _context;
public:
explicit StateManager(typename State<Id>::Context context);
};
#include "StateManager.inl"
}
StateManager.inl
template<typename Id>
StateManager<Id>::StateManager(typename State<Id>::Context context) :
_context(context)
{ }
State.h
#pragma once
namespace Game2D {
template<typename Id>
class StateManager;
template<typename Id>
class State {
public:
struct Context {
Context(int);
int data;
};
private:
StateManager<Id>* _manager;
Context _context;
public:
State(StateManager<Id>&, Context);
virtual ~State();
};
#include "State.inl"
}
State.inl
template<typename Id>
State<Id>::Context::Context(int data) {
this->data = data;
}
template<typename Id>
State<Id>::State(StateManager<Id>& manager, Context context) :
_manager(&manager),
_context(context)
{ }
template<typename Id>
State<Id>::~State() { }
Building this Project yields the following errors:
Error 10 error C1903: unable to recover from previous error(s); stopping compilation state.inl 9 1
Error 9 error C2065: 'context' : undeclared identifier state.inl 8 1
Error 7 error C2065: 'manager' : undeclared identifier state.inl 7 1
Error 8 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int state.inl 7 1
Error 6 error C2039: 'State' : is not a member of '`global namespace'' state.inl 6 1
Error 1 error C2143: syntax error : missing ';' before '<' state.inl 2 1
Error 2 error C2988: unrecognizable template declaration/definition state.inl 2 1
Error 3 error C2059: syntax error : '<' state.inl 2 1
Error 4 error C3083: 'Context': the symbol to the left of a '::' must be a type state.inl 2 1
Error 5 error C2039: 'Context' : is not a member of '`global namespace'' state.inl 2 1
Any help on how to fix these errors would be much appreciated!
A wild guess would be that you added your .inl files to you your project as standalone translation units and the compiler attempted to compile them as standalone translation units.
These files make no sense as standalone translation units and they will not compile as such. These are include files (aka header files). They are supposed to be seen as header files by the project. They are not supposed to be compiled directly.
Related
Im having a compilation error and i cant figure why.
I have enum declaration on the .h file and the .cpp file suppose to use it inside stringToEnum() function
this is the .cpp file
#include "A.h"
A::A(void)
{
}
A::~A(void)
{
}
values A::stringToEnum (string inputString) {
if (inputString == "string1") return val1;
if (inputString == "string2") return val2;
}
this is the header file
class A
{
public:
A(void);
~A(void);
private:
enum values{
val1,
val2,
val3,
val4
};
values stringToEnum (string inputString);
};
this is the error I get:
1>c:\users\documents\visual studio 2010\projects\A.cpp(25): error C2143: syntax error : missing ';' before 'A::stringToEnum'
1>c:\users\documents\visual studio 2010\projects\A.cpp(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\documents\visual studio 2010\projects\A.cpp(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\documents\visual studio 2010\projects\A.cpp(25): error C2556: 'int A::stringToEnum(std::string)' : overloaded function differs only by return type from 'A::values A::stringToEnum(std::string)'
1> c:\users\documents\visual studio 2010\projects\A.h(22) : see declaration of 'A::stringToEnum'
1>c:\users\documents\visual studio 2010\projects\A.cpp(25): error C2371: 'A::stringToEnum' : redefinition; different basic types
1> c:\users\documents\visual studio 2010\projects\A.h(22) : see declaration of 'A::stringToEnum'
I will be happy for some guidance.
thanks
Since values is contained in A, you need to qualify the name:
A::values A::stringToEnum (string inputString) {
//...
I'm currently practicing some linked list assignments out of my book right now and I'm stuck compiling errors. The implementation file seems to be fine, however the header file is what is receiving the errors.
Here is my header file:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <string>
using namespace std;
template<class T>
class linkedList {
public:
linkedList();
linkedList(const linkedList& copy);
~linkedList();
int getSize() const;
void addEntry(T entry);
bool deleteEntry(T entry);
T getEntry(int input) const;
linkedList operator=(const linkedList& right);
private:
struct node {
T data;
node<T> *next;
};
node<T> *linkedList = NULL;
};
#include "linkedlist.cpp"
#endif // LINKEDLIST_H
The errors I'm getting from my compiler is as follows:
c:\users\andym_000\documents\linkedlist\linkedlist.h(22) : error C2059: syntax error : '<'
c:\users\andym_000\documents\linkedlist\linkedlist.h(20) : see reference to class template instantiation 'linkedList<T>::node' being compiled
c:\users\andym_000\documents\linkedlist\linkedlist.h(25) : see reference to class template instantiation 'linkedList<T>' being compiled
c:\users\andym_000\documents\linkedlist\linkedlist.h(22) : error C2238: unexpected token(s) preceding ';'
c:\users\andym_000\documents\linkedlist\linkedlist.h(24) : error C2059: syntax error : '<'
c:\users\andym_000\documents\linkedlist\linkedlist.h(24) : error C2238: unexpected token(s) preceding ';'
c:\users\andym_000\documents\linkedlist\linkedList.h(24) : error C2059: syntax error : '<'
..\linkedList\main.cpp(9) : see reference to class template instantiation 'linkedList<T>' being compiled
with
[
T=std::string
]
c:\users\andym_000\documents\linkedlist\linkedList.h(24) : error C2238: unexpected token(s) preceding ';'
c:\users\andym_000\documents\linkedlist\linkedList.h(24) : error C2059: syntax error : '<'
..\linkedList\main.cpp(10) : see reference to class template instantiation 'linkedList<T>' being compiled
with
[
T=int
]
c:\users\andym_000\documents\linkedlist\linkedList.h(24) : error C2238: unexpected token(s) preceding ';'
Um, struct node {...} isn't a template. Your errors are telling you that is has no idea what to do with node<T>.
You use template notation with node class, which s not a template. T type is defined for whole linkedList - in definiition of node it is well defined type.
Note that node is defined inside linkedList, so it's qualified name will be linkedList<T>::node for linkedList<T> (i.e. each instantiation of linkedList has it's own node class).
So, just replace node<T> with node and that should work.
Why is this c++ program giving me errors:
#include <iostream>
using namespace std;
int main (){
NumbersClass num;
num.setNumbers(1);
}
class NumbersClass
{
public:
NumbersClass() {}
void setNumbers(int i) { }
};
Here are my errors:
taskbcplus.cpp(7): error C2065: 'NumbersClass' : undeclared identifier
taskbcplus.cpp(7): error C2146: syntax error : missing ';' before identifier 'num'
taskbcplus.cpp(7): error C2065: 'num' : undeclared identifier
taskbcplus.cpp(9): error C2065: 'num' : undeclared identifier
taskbcplus.cpp(9): error C2228: left of '.setNumbers' must have class/struct/union
1> type is ''unknown-type''
You need to put the NumberClass definition before the point at which you first instantiate it, i.e. before main.
class NumbersClass
{
public:
NumbersClass() {}
void setNumbers(int i) { }
};
int main (){
NumbersClass num;
num.setNumbers(1);
}
I was playing around with initializer lists and noticed some inconsistencies. Using an initializer_list as the first argument in a constructor causes a compilation error, but this doesn't happen for functions and member functions, or when other arguments precede the initializer_list.
This code illustrates what I mean:
#include <initializer_list>
using namespace std;
struct A
{
A(initializer_list<int> list){}
A(int a, initializer_list<int> list){}
A(initializer_list<int> list, int b){}
A(int a, initializer_list<int> list, int b){}
};
void B(initializer_list<int> list){}
void B(int a, initializer_list<int> list){}
void B(initializer_list<int> list, int b){}
void B(int a, initializer_list<int> list, int b){}
int main(int argc, char** argv)
{
// Ok
A a = {1};
A b(1, {2});
A c(1, {2}, 3);
B({1});
B(1, {2});
B({1}, 2);
B(1, {2}, 3);
// Error
A d({1});
A e({1}, 2);
return 0;
}
And the build output:
1>------ Build started: Project: Linal (Microsoft Visual C++ Compiler Nov 2012 CTP), Configuration: Release2 x64 ------
1> 'Microsoft Visual C++ Compiler Nov 2012 CTP' is for testing purposes only.
1> main.cpp
1>main.cpp(30): error C2059: syntax error : '{'
1>main.cpp(30): error C2059: syntax error : ')'
1>main.cpp(31): error C2059: syntax error : '{'
1>main.cpp(31): error C2143: syntax error : missing ';' before '}'
1>main.cpp(31): error C2143: syntax error : missing ')' before ';'
1>main.cpp(31): error C2059: syntax error : '}'
1>main.cpp(31): error C2059: syntax error : ')'
1>main.cpp(33): error C2059: syntax error : 'return'
1>main.cpp(34): error C2059: syntax error : '}'
1>main.cpp(34): error C2143: syntax error : missing ';' before '}'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Is this correct behaviour? If so, why does this happen?
I'm trying to compile Opengazer (Open source gaze tracker) code with visual studio on windows, while the code was originally written for linux and should be compile with cmake.
Anyway, I can't compile few files.
The code won't compile is this:
Containers.h:
#pragma once
#define xforeachactive(iter,container) \
for(typeof(container.begin()) iter = container.begin(); \
iter != container.end(); iter++) \
if ((*iter)->parent == this)
template <class ParentType, class ChildType> class Container;
template <class ParentType, class ChildType>
class Containee {
protected:
void detach() { parent = 0; }
public:
ParentType *parent; /* set to null to request removal */
Containee(): parent(0) {}
virtual ~Containee() {}
};
template <class ParentType, class ChildType>
class Container {
typedef ChildType *ChildPtr;
static bool isFinished(const ChildPtr &object) {
return !(object && object->parent);
}
protected:
std::vector<ChildPtr> objects;
void removeFinished() {
objects.erase(remove_if(objects.begin(), objects.end(), isFinished),
objects.end());
}
public:
void clear() {
xforeachactive(iter, objects)
(*iter)->parent = 0;
removeFinished();
}
static void addchild(ParentType *parent, const ChildPtr &child) {
parent->objects.push_back(child);
child->parent = parent;
parent->removeFinished();
}
virtual ~Container() {
clear();
}
};
template <class ParentPtr, class ChildPtr>
class ProcessContainer: public Container<ParentPtr, ChildPtr> {
public:
virtual void process() {
xforeachactive(iter, this->objects)
(*iter)->process();
this->removeFinished();
}
virtual ~ProcessContainer() {};
};
btw Containers.cpp is empty
ad the code uses the above class is:
#pragma once
class FrameProcessing;
class FrameFunction:
public Containee<FrameProcessing, FrameFunction>
{
const int &frameno;
int startframe;
protected:
int getFrame() { return frameno - startframe; }
public:
FrameFunction(const int &frameno): frameno(frameno), startframe(frameno) {}
virtual void process()=0;
virtual ~FrameFunction();
};
class FrameProcessing:
public ProcessContainer<FrameProcessing,FrameFunction> {};
class MovingTarget: public FrameFunction {
WindowPointer *pointer;
public:
MovingTarget(const int &frameno,
const vector<Point>& points,
WindowPointer *&pointer,
int dwelltime=20);
virtual ~MovingTarget();
virtual void process();
protected:
vector<Point> points;
const int dwelltime;
int getPointNo();
int getPointFrame();
bool active();
};
class CalibrationHandler
{
public:
CalibrationHandler(void);
~CalibrationHandler(void);
};
the error I get is :
visual studio 2008\projects\eyemouse\eyemouse\containers.h(58) : error C2146: syntax error : missing ';' before identifier 'iter'
visual studio 2008\projects\eyemouse\eyemouse\containers.h(58) : error C2065: 'iter' : undeclared identifier
visual studio 2008\projects\eyemouse\eyemouse\containers.h(58) : error C2065: 'iter' : undeclared identifier
visual studio 2008\projects\eyemouse\eyemouse\containers.h(58) : error C2146: syntax error : missing ')' before identifier 'iter'
visual studio 2008\projects\eyemouse\eyemouse\containers.h(58) : error C2059: syntax error : ';'
visual studio 2008\projects\eyemouse\eyemouse\containers.h(58) : error C2065: 'iter' : undeclared identifier
visual studio 2008\projects\eyemouse\eyemouse\containers.h(58) : error C2059: syntax error : ')'
visual studio 2008\projects\eyemouse\eyemouse\containers.h(58) : error C2143: syntax error : missing ';' before 'if'
visual studio 2008\projects\eyemouse\eyemouse\containers.h(58) : error C2065: 'iter' : undeclared identifier
visual studio 2008\projects\eyemouse\eyemouse\containers.h(58) : error C2227: left of '->parent' must point to class/struct/union/generic type
type is ''unknown-type''
visual studio 2008\projects\eyemouse\eyemouse\containers.h(59) : error C2065: 'iter' : undeclared identifier
visual studio 2008\projects\eyemouse\eyemouse\containers.h(59) : error C2227: left of '->process' must point to class/struct/union/generic type
type is ''unknown-type''
I understand why I'm getting an error.
'iter' is not defined anywhere. anyway, this isnt my code and it should work.
I tried to copy and past the define part to the function, but still get the same error.
I'm stuck with this and trying to solve it for hours, but can't understand what to do to make it work.
I'll really be grateful for any help.
typeof is a gcc extension and equivalent to C++0x decltype there is no VS version that actually supports it.
You would need to use C++0x and decltype or try to use Boost.TypeOf, which comes with its own caveats.
Change the macro to this:
#include <boost/typeof/typeof.hpp>
#define xforeachactive(iter,container) \
for(BOOST_TYPEOF(container.begin()) iter = container.begin(); \
iter != container.end(); iter++) \
if ((*iter)->parent == this)
You could also use BOOST_AUTO if you think this is clearer.