C++ Error in program compiling - c++

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

Related

C++ error C2143 syntax error : missing ';' before function name

I have header file :
#ifndef VIP_TICKET_H
#define VIP_TICKET_H
#include "ticket.h"
class VIPTicket : public Ticket
{
public:
enum VIPType { FIRST_CLASS, FAST_LINE };
VIPType getTicketType() const;
private:
VIPType type;
};
#endif
and it's cpp file
#include "vipTicket.h"
VIPType VIPTicket::getTicketType() const
{
return type;
}
the error says " error C2143: syntax error : missing ';' before 'VIPTicket::getTicketType' "
this error is very confusing.. i guess it's not a ';' that is missing but probably something else wrong with the code that I can't put my finger on..
The problem is this definition
VIPType VIPTicket::getTicketType() const
{
...
}
When you define this function you have to remember that VIPType is not in the global scope, but in the scope of the VIPTicket class, so you have to explicitly mention the scope:
VIPTicket::VIPType VIPTicket::getTicketType() const
{
...
}

Very Confused on Template INL File

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.

C++: std::initializer_list<T> as first argument in constructor causes compilation error

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?

Same code, errors when vector is changed to unordered_set

I have a class like:
class VectorAttrIterator : public AttrIterator {
vector<AttrValue>* values;
vector<AttrValue>::iterator it;
public:
VectorAttrIterator(vector<AttrValue>* _values) : values(_values) {
it = (*values).begin();
};
bool hasNext() {
return it != (*values).end();
};
AttrValue next() {
int ret = (*it);
it++;
return ret;
};
~VectorAttrIterator() {
delete values;
};
};
It works. Then I wanted to do something similar just for unordered_set:
class UnorderedSetAttrIterator : public AttrIterator {
unordered_set<AttrValue>* values;
unordered_set<AttrValue>::iterator it;
public:
UnorderedSetAttrIterator(vector<AttrValue>* _values) : values(_values) {
it = (*values).begin();
};
bool hasNext() {
return it != (*values).end();
};
AttrValue next() {
int ret = (*it);
it++;
return ret;
};
~UnorderedSetAttrIterator() {
delete values;
};
};
The only changes are vector is changed to unordered_set and class renaming. But I get errors like:
Error 42 error C2065: 'values' : undeclared identifier h:\dropbox\sch\cs3202\code\source\includes\iterator.h 40
Error 43 error C2228: left of '.begin' must have class/struct/union h:\dropbox\sch\cs3202\code\source\includes\iterator.h 40
Error 44 error C2440: 'initializing' : cannot convert from 'std::vector<_Ty> *' to 'std::tr1::unordered_set<_Kty> *' h:\dropbox\sch\cs3202\code\source\includes\iterator.h 39
Error 45 error C2439: 'UnorderedSetAttrIterator::values' : member could not be initialized h:\dropbox\sch\cs3202\code\source\includes\iterator.h 39
Error 46 error C2065: 'it' : undeclared identifier h:\dropbox\sch\cs3202\code\source\includes\iterator.h 44
Error 47 error C2065: 'values' : undeclared identifier h:\dropbox\sch\cs3202\code\source\includes\iterator.h 44
Error 48 error C2228: left of '.end' must have class/struct/union h:\dropbox\sch\cs3202\code\source\includes\iterator.h 44
Error 49 error C2065: 'it' : undeclared identifier h:\dropbox\sch\cs3202\code\source\includes\iterator.h 48
Error 50 error C2065: 'it' : undeclared identifier h:\dropbox\sch\cs3202\code\source\includes\iterator.h 49
Error 51 error C2065: 'values' : undeclared identifier h:\dropbox\sch\cs3202\code\source\includes\iterator.h 54
Whats wrong? Why is values undeclared? Full Source
It looks like this line is this problem:
UnorderedSetAttrIterator(vector<AttrValue>* _values) : values(_values) {
Have you forgotten to change the parameter type from vector to unordered_set?

Trying to compile a .h file without understanding something

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.