1 #include"unmanaged.h"
2 #include"stdafx.h"
3 using namespace std;
4 _gc class Mclass
5 {
6 private:
7 string Mx;
8 cppclass * obj;
9 public:
10 Mclass();
11 ~Mclass();
12 string native();
13 };
when buliding this throws
error C4430: missing type specifier - int assumed.
Note: C++ does not support default-int and
error C2143: syntax error : missing ';' before '
the content of unmanged.h is
#include"stdafx.h"
#include<string>
#include<iostream>
using namespace std;
class cppclass
{
private:
string x;
public:
cppclass();
~cppclass();
string native();
};
You need two underscores in __gc. See msdn.
Related
The Nodes.h file alone when compiled works, however when I include it in the Nodes.cpp file, all the errors arise, such as missing type specifier - int assumed at line 11,12,13 . Also another error is syntax error: identifier 'ASTSimpleExpressionNode'. Is it something that I am doing wrong. Can't I specify how my struct can be constructed by defining the different constructors?
For now the ASTSimpleExpressionNode is empty because if I continued the process it would duplicate all the errors.
Some of the errors:
Error C2535 'ASTExpressionNode::ASTExpressionNode(void)': member
function already defined or declared [line 16] Error C4430 missing
type specifier - int assumed. Note: C++ does not support
default-int [Line 11, 12,13 so on.] Error C2143 syntax error: missing
';' before '*' [Line 11,12,13] unexpected token(s) preceding ';' [Line
11,12,13]
Nodes.h file.
#pragma once
#include <string>
using namespace std;
struct ASTNode
{
};
struct ASTExpressionNode : ASTNode
{
ASTSimpleExpressionNode *left;
ASTRelationOperatorNode *rel_op;
ASTSimpleExpressionNode *right;
ASTExpressionNode(ASTSimpleExpressionNode *l);
ASTExpressionNode(ASTSimpleExpressionNode *l, ASTRelationOperatorNode *op, ASTSimpleExpressionNode *r);
};
struct ASTSimpleExpressionNode : ASTExpressionNode
{
};
struct ASTRelationOperatorNode :ASTExpressionNode
{
string rel_op;
ASTRelationOperatorNode(string op);
};
Nodes.cpp file.
#include "Nodes.h"
Forward-declare ASTSimpleExpressionNode and ASTRelationOperatorNode:
struct ASTSimpleExpressionNode;
struct ASTRelationOperatorNode;
struct ASTExpressionNode : ASTNode
{
// etc
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.
In Stroustrup, C++ Programming Language 4th Edition, he wrote the on page 79 the following code:
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <allocators>
using std::vector;
using namespace std;
template<class T>
class Vector : vector<T>
{
private:
T elem[50];
int size;
public:
***using vector<T>::vector; // inherit constructors***
T& operator[](size_type i) { check(i); return this−>elem(i); }
const T& operator=(size_type i) const {
check(i);
return this−>elem(i);
}
void check(size_type i) { if (this−>size()<i) throw Bad_index(i); }
};
int _tmain(int argc, _TCHAR* argv[])
{
Vector <int> V{ 1, 2, 3, 4 };
return 0;
}
When I compile the program, I receive the following errors:
Error 3 error C2440: 'initializing' : cannot convert from
'initializer-list' to 'Vector' c:\computer programming\c++
programming
language\code\ch3\consoleapplication3\consoleapplication3\consoleapplication3.cpp 28 1 ConsoleApplication3
Error 1 error C2886: 'vector>' : symbol cannot
be used in a member using-declaration c:\computer programming\c++
programming
language\code\ch3\consoleapplication3\consoleapplication3\consoleapplication3.cpp 17 1 ConsoleApplication3
Error 2 error C2886: 'vector>' : symbol cannot
be used in a member using-declaration c:\computer programming\c++
programming
language\code\ch3\consoleapplication3\consoleapplication3\consoleapplication3.cpp 17 1 ConsoleApplication3
4 IntelliSense: no instance of constructor "Vector::Vector [with
T=int]" matches the argument list
argument types are: (int, int, int, int) c:\Computer Programming\C++ Programming
Language\Code\Ch3\ConsoleApplication3\ConsoleApplication3\ConsoleApplication3.cpp 28 16 ConsoleApplication3
My question involves mostly Error 2 error C2886: which refers to the using directive. Visual Basic defines the error as follow:
'class::identifier' : symbol cannot be used in a member using-declaration
A using declaration uses a symbol, such as a namespace name. A using declaration is for declaring base class members.
Stroustrup apparently uses it but I have failed to duplicate his method. Is there a header I am missing or what? Could someone explain my error? thank you jmg.
Hi I am getting the following error when i am trying to compile my code.
error C2143: syntax error : missing ';' before '.' when i call
Points.addPoints();
Thanks for any help.
#ifndef _POINTS_H
#define _POINTS_H
//points.h
#include <sstream>
using namespace std;
class Points{
int pointsADD;
int pointsRemove;
int newPoints;
public :
Points(int points){this->pointsADD=points;this->pointsRemove=pointsRemove;this->newPoints=newPoints;}
void addPoints(int newPointsADD){
newPoints=pointsADD+newPointsADD++;
}
void removePoints(int newPointsRemove){
newPoints=pointsRemove+newPointsRemove--;
}
int getPoints(){
return newPoints;
}
};
#endif
You need an instance of the class to add anything. Read a good C++ introduction, and fix for now:
Points p(42);
p.addPoints(23);
The compiler is giving me the following complaints about the following class:
class AguiWidgetBase
{
//variables
AguiColor tintColor;
AguiColor fontColor;
//private methods
void zeroMemory();
virtual void onPaint();
virtual void onTintColorChanged(AguiColor color);
void (*onPaintCallback)(AguiRectangle clientRect);
void (*onTintColorChangedCallback)();
public:
AguiWidgetBase(void);
~AguiWidgetBase(void);
void paint();
void setTintColor(AguiColor color);
AguiColor getBackColor();
};
Warning 13 warning C4183: 'getBackColor': missing return type; assumed to be a member function returning 'int' c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 26
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 11
Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 11
Error 5 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 12
Error 6 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 12
Error 11 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 26
Error 12 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 26
Error 1 error C2146: syntax error : missing ';' before identifier 'tintColor' c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 11
Error 10 error C2146: syntax error : missing ';' before identifier 'getBackColor' c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 26
Error 4 error C2146: syntax error : missing ';' before identifier 'fontColor' c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 12
Error 8 error C2061: syntax error : identifier 'AguiRectangle' c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 17
Error 7 error C2061: syntax error : identifier 'AguiColor' c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 16
Error 9 error C2061: syntax error : identifier 'AguiColor' c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 25
This should be working, I'm including the headers for those classes.
This is the h file:
//integer Point class
class AguiPoint {
int x;
int y;
public:
int getX();
int getY();
void setX(int x);
void setY(int y);
void set(int x, int y);
AguiPoint(int x, int y);
AguiPoint();
std::string toString();
std::string xToString();
std::string yToString();
};
//floating version of Agui Point
class AguiPointf {
float x;
float y;
public:
float getX();
float getY();
void setX(float x);
void setY(float y);
void set(float x, float y);
AguiPointf(float x, float y);
AguiPointf(AguiPoint p);
AguiPointf();
std::string toString();
std::string xToString();
std::string yToString();
};
//Integer rectangle class
class AguiRectangle {
int x;
int y;
int width;
int height;
public:
bool isEmpty();
int getTop();
int getLeft();
int getBottom();
int getRight();
AguiPoint getTopLeft();
AguiPoint getBottomRight();
};
class AguiColor {
unsigned char r;
unsigned char g;
unsigned char b;
unsigned char a;
void verifyColorBounds();
public:
AguiColor(int r, int g, int b, int a);
AguiColor(float r, float g, float b, float a);
AguiColor();
int getR();
int getG();
int getB();
int getA();
};
Thanks
I include the main header in the WidgetBase and the main header includes the base types before it includes the widgetbase
It seems you are not including the headers in the right order.
C++ is very sensitive to the order in which identifiers are declared. The compiler will process a source file (and any #include-s it finds along the way) in a sequential order and for every (non-builtin) identifier, the compiler must have seen a declaration before you can use it.
If you already have the headers included i'd guess you need to fully qualify the names because of a namespace or that Aguiwidgitbase is in the wrong namespace? either way check the namespaces in the headers you included
Check header files and namespaces. You may also need forward declarations.
You may have a problem of include dependencies. Header guards preventing multiple definition (#pragma once, or #ifndef HEADER #define HEADER) can block your inclusion.
One answer can be forward declarations... for pointers to class, not for class members though (need to know storage size at compile time).