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) {
//...
Related
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.
Please have a look at the following code,
Stack.h
template <typename T>
class Stack
{
public:
Stack(int number)
{
maxSize = number;
top = -1;
stackData = new T*[maxSize];
}
~Stack()
{
delete [] stackData;
}
int count()
{
}
bool isEmpty()
{
if(top==-1)
{
return true;
}
else
{
return false;
}
}
bool isFull()
{
if(top== (maxSize-1))
{
return true;
}
else
{
return false;
}
}
*T pop()
{
if(!isEmpty())
{
return stackData[top--]; // Remove Item From Stack
}
}
*T peek()
{
T *peekData = &stackData[top];
return peekData;
}
void push(T *pushValue)
{
if(!isFull())
{
stackData[++top] = pushValue;
}
}
private:
int maxSize;
T ** stackData;
int top;
};
Main.cpp
#include <iostream>
#include "Stack.h"
#include <iostream>
using namespace std;
int main()
{
int i = 0;
Stack<double> doubleStack(5);
double doubleValue = 1.1;
cout << "pushing elements into the stack" << endl;
while(i<5)
{
doubleStack.push();
}
system("pause");
return 0;
}
When I run this code, I get the following error.
1>------ Build started: Project: CourseWork2, Configuration: Debug Win32 ------
1> Main.cpp
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(48): error C2146: syntax error : missing ';' before identifier 'pop'
1> c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(74) : see reference to class template instantiation 'Stack<T>' being compiled
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(48): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(49): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(54): warning C4183: 'pop': missing return type; assumed to be a member function returning 'int'
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(56): error C2146: syntax error : missing ';' before identifier 'peek'
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(56): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(57): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(60): warning C4183: 'peek': missing return type; assumed to be a member function returning 'int'
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(48): error C2146: syntax error : missing ';' before identifier 'pop'
1> c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\main.cpp(11) : see reference to class template instantiation 'Stack<T>' being compiled
1> with
1> [
1> T=double
1> ]
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(48): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(49): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(49): warning C4183: 'pop': missing return type; assumed to be a member function returning 'int'
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(56): error C2146: syntax error : missing ';' before identifier 'peek'
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(56): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(57): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(57): warning C4183: 'peek': missing return type; assumed to be a member function returning 'int'
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\main.cpp(18): error C2660: 'Stack<T>::push' : function does not take 0 arguments
1> with
1> [
1> T=double
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Intellelisense is not identifying any method except isFull(), count() and isEmpty(). I can't code the rest because of this!
Why is this? Please help!
You put * at wrong place in function syntax:
Update:
*T pop()
*T peek()
To:
T* pop()
T* peek()
You put the asterisk in the wrong spot. It should be:
T *pop() {
//implementation
}
and
T *peek() {
//implementation
}
I have the following code :
class v8NMatch : public V8Wrap<Match, v8NMatch>
{
public:
static v8::Persistent<v8::FunctionTemplate> s_ct;
static void Init(v8::Handle<v8::Object> target);
static v8::Handle<v8::Value> IsSuccess(Arguments& args);
v8NMatch(const v8::Arguments& args);
};
It tells me I have a syntax error at the identifier Arguments.
If I put the const keyword before Arguments, I have 2 errors :
missing type specifier - int assumed. Note: C++ does not support default-int
syntax error : missing ',' before '&'
Any clue ?
IsSuccess is defined without the v8::Arguments.
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).
I'm having some trouble overloading my stream extraction operator in C++ for a hw assignment. I'm not really sure why I am getting these compile errors since I thought I was doing it right... Here is my code:
Complex.h
#ifndef COMPLEX_H
#define COMPLEX_H
class Complex
{
//friend ostream &operator<<(ostream &output, const Complex &complexObj) const;
// note at bottom regarding friend function
public:
Complex(double = 0.0, double = 0.0); // constructor
Complex operator+(const Complex &) const; // addition
Complex operator-(const Complex &) const; // subtraction
void print() const; // output
private:
double real; // real part
double imaginary; // imaginary part
};
#endif
Complex.cpp
#include <iostream>
#include "Complex.h"
using namespace std;
// Constructor
Complex::Complex(double realPart, double imaginaryPart) : real(realPart), imaginary(imaginaryPart)
{
}
// addition operator
Complex Complex::operator+(const Complex &operand2) const
{
return Complex(real + operand2.real, imaginary + operand2.imaginary);
}
// subtraction operator
Complex Complex::operator-(const Complex &operand2) const
{
return Complex(real - operand2.real, imaginary - operand2.imaginary);
}
// Overload << operator
ostream &Complex::operator<<(ostream &output, const Complex &complexObj) const
{
cout << '(' << complexObj.real << ", " << complexObj.imaginary << ')';
return output; // returning output allows chaining
}
// display a Complex object in the form: (a, b)
void Complex::print() const
{
cout << '(' << real << ", " << imaginary << ')';
}
main.cpp
#include <iostream>
#include "Complex.h"
using namespace std;
int main()
{
Complex x;
Complex y(4.3, 8.2);
Complex z(3.3, 1.1);
cout << "x: ";
x.print();
cout << "\ny: ";
y.print();
cout << "\nz: ";
z.print();
x = y + z;
cout << "\n\nx = y + z: " << endl;
x.print();
cout << " = ";
y.print();
cout << " + ";
z.print();
x = y - z;
cout << "\n\nx = y - z: " << endl;
x.print();
cout << " = ";
y.print();
cout << " - ";
z.print();
cout << endl;
}
Compile erros:
complex.cpp(23) : error C2039: '<<' : is not a member of 'Complex'
complex.h(5) : see declaration of 'Complex'
complex.cpp(24) : error C2270: '<<' : modifiers not allowed on nonmember functions
complex.cpp(25) : error C2248: 'Complex::real' : cannot access private member declared in class 'Complex'
complex.h(13) : see declaration of 'Complex::real'
complex.h(5) : see declaration of 'Complex'
complex.cpp(25) : error C2248: 'Complex::imaginary' : cannot access private member declared in class 'Complex'
complex.h(14) : see declaration of 'Complex::imaginary'
complex.h(5) : see declaration of 'Complex'
Thanks!
Edit:
I wasn't sure about declaring the friend function in the header file or not. When I do, I get these errors:
c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C2143: syntax error : missing ';' before '&'
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C2433: 'ostream' : 'friend' not permitted on data declarations
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C2061: syntax error : identifier 'ostream'
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C2805: binary 'operator <<' has too few parameters
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.cpp(23) : error C2872: 'ostream' : ambiguous symbol
1> could be 'c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : int ostream'
1> or 'c:\program files\microsoft visual studio 9.0\vc\include\iosfwd(708) : std::ostream'
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.cpp(23) : error C2143: syntax error : missing ';' before '&'
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.cpp(23) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.cpp(23) : error C2086: 'int ostream' : redefinition
1> c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : see declaration of 'ostream'
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.cpp(23) : error C2872: 'ostream' : ambiguous symbol
1> could be 'c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : int ostream'
1> or 'c:\program files\microsoft visual studio 9.0\vc\include\iosfwd(708) : std::ostream'
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.cpp(23) : error C2872: 'ostream' : ambiguous symbol
1> could be 'c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : int ostream'
1> or 'c:\program files\microsoft visual studio 9.0\vc\include\iosfwd(708) : std::ostream'
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.cpp(23) : error C2065: 'output' : undeclared identifier
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.cpp(23) : error C2059: syntax error : 'const'
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.cpp(24) : error C2143: syntax error : missing ';' before '{'
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.cpp(24) : error C2447: '{' : missing function header (old-style formal list?)
1>Generating Code...
1>Compiling...
1>main.cpp
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C2143: syntax error : missing ';' before '&'
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C2433: 'ostream' : 'friend' not permitted on data declarations
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C2061: syntax error : identifier 'ostream'
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C2805: binary 'operator <<' has too few parameters
Your commented-out declaration in the header is almost correct. Since it is a friend, it is not a member and thus cannot be const, so it should be:
friend std::ostream &operator<<(std::ostream &output, const Complex &complexObj);
Note also that you need to qualify ostream as std::ostream, since you should not use using namespace std; in a header file (you really shouldn't need to use it anywhere; it's generally better to just write out std:: when you want to use something from the standard library).
Likewise, in your source file, since it is not a member function, you do not prefix the operator definition with the class name, and it should be:
std::ostream &operator<<(std::ostream &output, const Complex &complexObj)
If you want client code to have access to the operator<< overload, add
ostream & operator<<(ostream &output, const Complex &complexObj) const
to your class header file.
If, on the other hand, you only want client code to call print(), remove the
Complex::
scoping from the operator definition in the implementation file.