I'm trying to port a program/game that was developed in linux/g++ so that it runs on windows 7 VS 2008 C++. The program uses libraries such as OpenGL, SDL, and Boost.
I've solved a lot of errors but I'm kind of stuck on this one.
I get error code C2059 and C2238. The actual error messages are:
------ Build started: Project: Asteroid Blaster, Configuration: Release Win32 ------
Compiling...
WeaponDisplay.cpp
Weapon.cpp
ViewFrustum.cpp
Vector3D.cpp
UDP_Server.cpp
d:\College\AsteroidBlaster\Utility/ViewFrustum.h(40) : error C2059: syntax error : ';'
d:\College\AsteroidBlaster\Utility/ViewFrustum.h(40) : error C2238: unexpected token(s) preceding ';'
d:\College\AsteroidBlaster\Utility/ViewFrustum.h(41) : error C2059: syntax error : ';'
d:\College\AsteroidBlaster\Utility/ViewFrustum.h(41) : error C2238: unexpected token(s) preceding ';'
UDP_Client.cpp
d:\College\AsteroidBlaster\Utility/ViewFrustum.h(40) : error C2059: syntax error : ';'
d:\College\AsteroidBlaster\Utility/ViewFrustum.h(40) : error C2238: unexpected token(s) preceding ';'
d:\College\AsteroidBlaster\Utility/ViewFrustum.h(41) : error C2059: syntax error : ';'
d:\College\AsteroidBlaster\Utility/ViewFrustum.h(41) : error C2238: unexpected token(s) preceding ';'
TractorBeamShot.cpp
The ViewFrustum.h file is as follows:
/**
* viewFrustum: It's got useful functions to cull items that aren't in your view frustum
*
* 2-7-11
* CPE ---
*/
#ifndef __VIEW_FRUSTUM_H__
#define __VIEW_FRUSTUM_H__
#include "Items/Object3D.h"
#include "Utility/Plane.h"
#include "Utility/Matrix4.h"
#include <vector>
#include <set>
class ViewFrustum {
public:
ViewFrustum();
virtual ~ViewFrustum();
/* Takes in a list of all the Object 3D's around, and culls them down to only the ones
* that are inside the view frustum.
*/
virtual std::list<Drawable*>* cullToViewFrustum(std::vector<Drawable*>* all, bool skipParticles);
virtual std::list<Object3D*>* cullToViewFrustum(std::vector<Object3D*>* all);
/* Prints out details about all of the planes of the view frustum.
*/
virtual void print();
private:
Matrix4 projMatrix;
Matrix4 modelViewMatrix;
Plane* top;
Plane* bottom;
Plane* left;
Plane* right;
Plane* near;
Plane* far;
/**
* A modified version of chedDrawableOutside, used by the AI. This stops the
* AI from targeting Drawable objects which are slightly outside the field
* of view, which still need to be drawn.
*/
bool checkTargetableOutside(Drawable* obj);
/* Returns true if the Drawable object is completely outside of the
* view frustum planes.
* Returns false if it's even part-way inside.
*/
virtual bool checkDrawableOutside(Drawable* obj);
};
#endif
The offending lines (40-41) are:
Plane* near;
Plane* far;
It's kind of confusing because the ViewFrustum obj got compiled already but when it gets to UDP_Server.cpp and UDP_Client.cpp, it throws an error. I'm not sure if it matters but the UDP classes use Boost's aiso and serialization modules. I know Boost sometimes gives out weird warnings but I'm not sure if Boost has to do anything with this error.
If anyone has any idea why this is or if you need more code posted, please let me know.
Chances are you're including a windows header before that one, and near and far are defined somehwere in there. Those are (legacy) things from the 16bit era.
See this related question Is there a clean way to prevent windows.h from creating a near & far macro? for workarounds.
Related
I am working on Visual Studio 2015 community edition.
The following code produces an error:
#include <list>
using std::list;
template <typename T>
class enumerator
{
// typedef:
public:
using list_ = list<T*>; // line:
using list_fwd_c = list_::const_iterator; // 188
using list_fwd = list_::iterator; // 189
using list_rvs_c = list_::const_reverse_iterator; // 190
using list_rvs = list_::reverse_iterator; // 191
}; // 192
and the error:
1>d:\develop\workspace\visual_studio\nevada_test_site\source\shared\cpp\expression_evaluator\expression_evaluator\src\expr_eval.cpp(188): error C2061: syntax error: identifier 'const_iterator'
1> d:\develop\workspace\visual_studio\nevada_test_site\source\shared\cpp\expression_evaluator\expression_evaluator\src\expr_eval.cpp(192): note: see reference to class template instantiation 'cpplib::eval::utils::enumerator<T>' being compiled
1>d:\develop\workspace\visual_studio\nevada_test_site\source\shared\cpp\expression_evaluator\expression_evaluator\src\expr_eval.cpp(188): error C2238: unexpected token(s) preceding ';'
1>d:\develop\workspace\visual_studio\nevada_test_site\source\shared\cpp\expression_evaluator\expression_evaluator\src\expr_eval.cpp(189): error C2061: syntax error: identifier 'iterator'
1>d:\develop\workspace\visual_studio\nevada_test_site\source\shared\cpp\expression_evaluator\expression_evaluator\src\expr_eval.cpp(189): error C2238: unexpected token(s) preceding ';'
1>d:\develop\workspace\visual_studio\nevada_test_site\source\shared\cpp\expression_evaluator\expression_evaluator\src\expr_eval.cpp(190): error C2061: syntax error: identifier 'const_reverse_iterator'
1>d:\develop\workspace\visual_studio\nevada_test_site\source\shared\cpp\expression_evaluator\expression_evaluator\src\expr_eval.cpp(190): error C2238: unexpected token(s) preceding ';'
1>d:\develop\workspace\visual_studio\nevada_test_site\source\shared\cpp\expression_evaluator\expression_evaluator\src\expr_eval.cpp(191): error C2061: syntax error: identifier 'reverse_iterator'
1>d:\develop\workspace\visual_studio\nevada_test_site\source\shared\cpp\expression_evaluator\expression_evaluator\src\expr_eval.cpp(191): error C2238: unexpected token(s) preceding ';'
I have confirmed this is the troublesome code piece (error disappear when I comment out the 'enumerator' class).
As its name suggests, it "will be" a simple enumerator class (something like a IEnumerator from C#. I will use it in order to store a list of objects derived from a base class passed as a class template parameter T.
Question:
1. Why the code above produces this error.
2. How can I fix this error?
3. I know this is not a place for this, but if you know a good implementation (boost ?) for this kind of enumerator, please point me in the right direction.
You still need to add a typename despite how convenient using is:
using list_fwd_c = typename list_::const_iterator; // 188
using list_fwd = typename list_::iterator; // 189
using list_rvs_c = typename list_::const_reverse_iterator; // 190
using list_rvs = typename list_::reverse_iterator; // 191
The problem is that these are dependent types (they depend on the template parameter) so the compiler cannot just know they are all types (and not, say functions or static data members)
I'm working on a little C++ WinAPI wrapper. I'd like to create my own window and append my own controls to it.
I created 2 classes : Window and Control.
The problem is that I struggle to deal with the multiple header files, because the class Control needs the class Window and vice versa. When I compile my code, I get many errors..
Here are my files :
globals.h :
// globals.h
// I need this file to define a few constants and to include the main headers needed by my classes
#ifndef GLOBALS_H
#define GLOBALS_H
#include <windows.h>
#include <iostream>
#include <vector>
#include "window.h"
#define SOME_GLOBAL_CONSTANT 1
#endif
window.h :
// window.h
#ifndef WINDOW_H
#define WINDOW_H
#include "globals.h"
#include "control.h"
class Window
{
public:
Window(RECT windowRect);
virtual ~Window();
void appendChild(Control* child);
private:
RECT m_windowRect;
Control m_staticBackground;
std::vector<Control*> m_children;
};
#endif
window.cpp :
// window.cpp
#include "window.h"
Window::Window(RECT windowRect) : m_windowRect(windowRect)
{
std::cout << SOME_GLOBAL_CONSTANT << std::endl;
}
Window::~Window()
{
m_children.clear();
}
void Window::appendChild(Control* child)
{
m_children.push_back(child);
}
control.h :
// control.h
#ifndef CONTROL_H
#define CONTROL_H
#include "globals.h"
class Control
{
public:
Control(Window* parentWindow);
virtual ~Control();
private:
RECT m_controlRect;
Window* m_parentWindow;
};
#endif
control.cpp :
// control.cpp
#include "control.h"
Control::Control(Window* parentWindow) : m_controlRect({}), m_parentWindow(parentWindow)
{
std::cout << SOME_GLOBAL_CONSTANT << std::endl;
}
Control::~Control()
{}
And finally, main.cpp :
#include "globals.h"
class MyCustomControl : public Control
{
public:
MyCustomControl(Window* parentWindow) : Control(parentWindow)
{}
~MyCustomControl()
{}
};
class MyWindow : public Window
{
public:
MyWindow(RECT windowRect) : Window(windowRect)
{
kid1 = new MyCustomControl(this);
appendChild(kid1);
}
~MyWindow()
{
delete kid1;
}
private:
MyCustomControl* kid1;
};
int main()
{
MyWindow appWindow;
std::cout << SOME_GLOBAL_CONSTANT << std::endl;
return 0;
}
Here are all the compilation errors I get :
1>------ Build started: Project: include, Configuration: Debug Win32 ------
1>window.cpp
1>d:\visual studio 2017\projects\include\include\control.h(9): error C2061: syntax error: identifier 'Window'
1>d:\visual studio 2017\projects\include\include\control.h(14): error C2143: syntax error: missing ';' before '*'
1>d:\visual studio 2017\projects\include\include\control.h(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\visual studio 2017\projects\include\include\control.h(14): error C2238: unexpected token(s) preceding ';'
1>main.cpp
1>d:\visual studio 2017\projects\include\include\control.h(9): error C2061: syntax error: identifier 'Window'
1>d:\visual studio 2017\projects\include\include\control.h(14): error C2143: syntax error: missing ';' before '*'
1>d:\visual studio 2017\projects\include\include\control.h(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\visual studio 2017\projects\include\include\control.h(14): error C2238: unexpected token(s) preceding ';'
1>d:\visual studio 2017\projects\include\include\main.cpp(8): error C2664: 'Control::Control(const Control &)': cannot convert argument 1 from 'Window *' to 'const Control &'
1>d:\visual studio 2017\projects\include\include\main.cpp(8): note: Reason: cannot convert from 'Window *' to 'const Control'
1>d:\visual studio 2017\projects\include\include\main.cpp(8): note: No constructor could take the source type, or constructor overload resolution was ambiguous
1>d:\visual studio 2017\projects\include\include\main.cpp(32): error C2512: 'MyWindow': no appropriate default constructor available
1>d:\visual studio 2017\projects\include\include\main.cpp(13): note: see declaration of 'MyWindow'
1>control.cpp
1>d:\visual studio 2017\projects\include\include\window.h(13): error C2061: syntax error: identifier 'Control'
1>d:\visual studio 2017\projects\include\include\window.h(17): error C3646: 'm_staticBackground': unknown override specifier
1>d:\visual studio 2017\projects\include\include\window.h(17): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\visual studio 2017\projects\include\include\window.h(18): error C2065: 'Control': undeclared identifier
1>d:\visual studio 2017\projects\include\include\window.h(18): error C2059: syntax error: '>'
1>d:\visual studio 2017\projects\include\include\window.h(18): error C2976: 'std::vector': too few template arguments
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.25017\include\vector(700): note: see declaration of 'std::vector'
1>Generating Code...
1>Done building project "include.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Any help would be greatly appreciated.
Thank you in advance,
winapiwrapper.
Edit: I have also read this thread, but I couldn't solve my problem.
Welcome to programming. Strike down one error message and two shall take its place.
Well... Not really. What's happening is as you resolve one error, this reveals the errors that the precious errors were hiding.
First a PSA:
Don't write much code without compiling and testing. This way you have a smaller amount of code you need to check when something goes wrong. Start with a main function. Make sure it builds. Add the headers you need. Make sure it builds. Write a function for main to call. Make sure it builds. Repeat until program is finished. Trying to add too much all at once results in cascading storms of errors like you have experienced here.
Global include header files are usually a sucker bet. They often lead you into problems like the circular dependency you have here and make files include everything even when they don't have to. This can slow down build times.
Next, read the following link before continuing: Resolve header include circular dependencies
Now on with the answer:
window.h includes control.h. control .h includes window.h though global.h. This results in the Chicken and egg problem discussed in the above link. One of the headers is going to be included before the other and not be able to find the contents of the other file. Fortunately control.h only needs a reference to Window, not the whole thing, and this can be satisfied with a forward declaration and removing the global include file.
I'm not going to demonstrate cleaning this up. The process is well documented in the link.
This exposes hydra head number 2: Window contains Control m_staticBackground and does not explicitly initialize it. This results in the compiler hunting around for a default constructor for Control, something that does not exist.
Solution: Explicitly initialize m_staticBackground
Window::Window(RECT windowRect) : m_windowRect(windowRect),
m_staticBackground(this)
{
std::cout << SOME_GLOBAL_CONSTANT << std::endl;
}
BIG Mother Freaking note here: m_staticBackground(this) is dodgy as hell. this has not been fully constructed yet, so if you do more than simply store it in Control::Control (which is all you are currently doing) very bad, unpredictable things can happen. Do not use parentWindow or m_parentWindow inside the body of the Control constructor. If possible find a better, safer, way to do this. If not possible, document it with DO NOT USE messages to remind your future self or anyone else looking at the code not to use them.
Once this has been fixed you get to
MyWindow appWindow;
over in main. MyWindow's constructor requires a RECT you don't currently have a RECT to provide, so I'll stop here.
Move "window.h" from globals.h to controls.cpp. And put class Window; before class Controls { in controls.h. This is called forward declaration.
You even do not need window.h in controls.cpp, you can move it directly to your main.cpp.
The code:
class FString : public StringFormatter<FString> {
public:
FString()
: StringFormatter<FString>{this->buffer}
{}
// allow assignment and construction from std::string.
FString(const std::string &other)
:
StringFormatter<FString>{this->buffer},
buffer{other} {}
gives a syntax error at
StringFormatter<FString>{this->buffer}
Converting it to
StringFormatter<FString>(this->buffer) works.
Errors:
../util/stringformatter.h(250): error C2059: syntax error: '{' ../util/stringformatter.h(250): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
../util/stringformatter.h(256): error C2059: syntax error: ','
../util/stringformatter.h(257): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
Here is the concerned file: https://github.com/synth2014/openage/blob/master/libopenage/util/stringformatter.h#L250
Looks like a bug in MSVC.
You can either use a typedef/alias:
using base = StringFormatter<FString>;
FString() : base{this->buffer} {}
or part with the braced-init-list:
FString() : StringFormatter<FString>(this->buffer) {}
to work around the issue.
I'm reading this article on the Boost Unit Testing Framework.
However I'm having a bit of trouble with the first example, my guess is that they left something out (something that would be obvious to hardcore C++ coders) as IBM often does in their articles. Another possibility is that my Visual Studio 2005 C++ compiler is just too old for the example.
#include "stdafx.h"
#define BOOST_TEST_MODULE stringtest
#include <boost/test/unit_test.hpp>
//#include "mystring.h"
BOOST_AUTO_TEST_SUITE(stringtest) // name of the test suite is stringtest
BOOST_AUTO_TEST_CASE(test1)
{
/*
mystring s;
BOOST_CHECK(s.size() == 0);
*/
BOOST_CHECK(0 == 0);
}
BOOST_AUTO_TEST_CASE(test2)
{
/*
mystring s;
s.setbuffer("hello world");
BOOST_REQUIRE_EQUAL('h', s[0]); // basic test
*/
BOOST_CHECK(0 == 0);
}
BOOST_AUTO_TEST_SUITE_END()
To me the BOOST_AUTO_TEST_SUITE and BOOST_AUTO_TEST_CASE lines look a little suspect (especially since they don't have quotes around the arguments, and they are undeclared identifiers...but this probably means they are macros and I'm not certain I understand the concept or if that is available in VC++ 8.0)...
#ifdef _MYSTRING
#define _MYSTRING
class mystring {
char* buffer;
int length;
public:
void setbuffer(char* s) { buffer s = s; length = strlen(s); }
char& operator[ ] (const int index) { return buffer[index]; }
int size() {return length; }
}
#endif
Is there any reason why this code won't work?
1>c:\users\andy\documents\visual studio 2005\projects\unittesttests\unittesttests\unittesttests.cpp(7) : error C2065: 'stringtest' : undeclared identifier
1>c:\users\andy\documents\visual studio 2005\projects\unittesttests\unittesttests\unittesttests.cpp(9) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\andy\documents\visual studio 2005\projects\unittesttests\unittesttests\unittesttests.cpp(9) : error C2146: syntax error : missing ';' before identifier 'BOOST_AUTO_TEST_CASE'
1>c:\users\andy\documents\visual studio 2005\projects\unittesttests\unittesttests\unittesttests.cpp(9) : error C2065: 'test1' : undeclared identifier
1>c:\users\andy\documents\visual studio 2005\projects\unittesttests\unittesttests\unittesttests.cpp(10) : error C2448: 'BOOST_AUTO_TEST_CASE' : function-style initializer appears to be a function definition
1>c:\users\andy\documents\visual studio 2005\projects\unittesttests\unittesttests\unittesttests.cpp(18) : error C2065: 'test2' : undeclared identifier
1>c:\users\andy\documents\visual studio 2005\projects\unittesttests\unittesttests\unittesttests.cpp(19) : error C2448: 'BOOST_AUTO_TEST_CASE' : function-style initializer appears to be a function definition
1>c:\users\andy\documents\visual studio 2005\projects\unittesttests\unittesttests\unittesttests.cpp(29) : fatal error C1004: unexpected end-of-file found
Looks correct to me. My Boost.Test code looks the same way. I'm running VS2008, but I know it works in 2005 as well.
Seems like your problem lies elsewhere.
If you use precompiled headers (and why do you do that in such a small test program?), shouldn't stdafx.h be included as the very first thing in the file?
And what is the first line for? You don't seem to use it, and _MYSTRING is a reserved name in C++ (everything that begins with underscore followed by a capital letter is off limits)
I've created a header file called "list_dec.h", put it in a folder "C:\Headers", and set my compiler to include files from "C:\Headers", so now I can do things like
#include<list_dec.h>
int main(){return(0);}
but when I try to do something like
#include<iostream>
#include<list_dec.h>
int main(){return(0);}
I get an error (not anything specific, just a huge list of syntax errors in "list_dec.h", which I know aren't real because I've been able to compile it as both a main.cpp file and a .h file in a separate project). However, when I change to order so "list_dec.h" is on top:
#include<list_dec.h>
#include<iostream>
int main(){return(0);}
all of the errors go away. So why does the order of the error matter?
NB: As far as I know, this occurs when I use "list_dec.h" with all header files, but the files I'm absolutely positive it occurs in are:
#include<iostream>
#include<vector>
#include<time.h>
#include<stdlib.h>
EDIT: These are the errors I get when "list_dec.h" is below any other header:
c:\headers\list_dec.h(14) : error C2143: syntax error : missing ')' before 'constant'
c:\headers\list_dec.h(51) : see reference to class template instantiation 'list<T,limit>' being compiled
c:\headers\list_dec.h(14) : error C2143: syntax error : missing ';' before 'constant'
c:\headers\list_dec.h(14) : error C2059: syntax error : ')'
c:\headers\list_dec.h(14) : error C2238: unexpected token(s) preceding ';'
c:\headers\list_dec.h(69) : warning C4346: 'list<T,limit>::{ctor}' : dependent name is not a type
prefix with 'typename' to indicate a type
c:\headers\list_dec.h(69) : error C2143: syntax error : missing ')' before 'constant'
c:\headers\list_dec.h(69) : error C2143: syntax error : missing ';' before 'constant'
c:\headers\list_dec.h(69) : error C2988: unrecognizable template declaration/definition
c:\headers\list_dec.h(69) : error C2059: syntax error : 'constant'
c:\headers\list_dec.h(69) : error C2059: syntax error : ')'
c:\headers\list_dec.h(78) : error C2065: 'T' : undeclared identifier
c:\headers\list_dec.h(78) : error C2065: 'limit' : undeclared identifier
c:\headers\list_dec.h(78) : error C2065: 'T' : undeclared identifier
c:\headers\list_dec.h(78) : error C2065: 'limit' : undeclared identifier
c:\headers\list_dec.h(79) : error C2143: syntax error : missing ';' before '{'
c:\headers\list_dec.h(79) : error C2447: '{' : missing function header (old-style formal list?)
If it helps, these are the lines mentioned in the errors (14, 69, 78, and 79):
Line 14: list(const T& NULL); (A constructor for "list" class)
Line 69: inline list<T, limit>::list(const T& NULL): (Definition for the constructor, also, the colon at the end is intentional, It part of the definion ie: void x(int n): VAR(n).)
Line 78: inline list<T, limit>::list(const list<T, limit>& lst) (def for the copy constructor)
Line 79: { (the begining of the list-copy contructor)
And a lot of people want to see the beginning of "list_dec.h":
template<class T, size_t limit>
class list
NB: These aren't the first lines, but they're where I think the problem is, the lines before them are simply an enumeration called "err".
EDIT: Just a note, "list_dec.h" contains no includes, defines, ifdefs, or anything precede with a '#'. Besides the enumeration, it only contains the "list" class declaration and the "list" class member function definitions.
Generally speaking it should not, however it may be possible for there to be conflicting definitions of symbols or preprocessor macros that end up confusing the compiler. Try to narrow down the size of the problem by removing pieces and includes from the conflicting header until you can see what is causing it.
In response to the error messages you posted, the symbol NULL is often implemented as a preprocessor macro for the number 0. This is so that you can easily use it as a null pointer. Therefore this:
list(const T& NULL);
Could be converted into this syntax error by the preprocessor:
list(const T& 0);
Change the name of the parameter to something other than NULL.
Note that here:
Line 14: list(const T& NULL); (A constructor for "list" class)
NULL is the name of standard macro - when a standard header file is included before list_dec.h it will most likely cause NULL to be defined which will in turn cause your code to look something like this to the compiler:
list(const T& 0);
The constant 0 above makes the line ill-formed C++. You might get more information by instructing your compiler to produce preprocessed output file.
Presumably list_dec.h is running into a macro that's defined in those other headers (or some headers they in turn include) -- hard to say which one without seeing the first error message and the relevant part of list_dec.h!
The actual errors would give a more specific clue, bt it means there's something in your include file that is screwing up the scan for the next one. The most common thing would be some kind of unclude #-directive, like a #if missing its #endif.
If the errors are random in nature, it could be a missing semi colon. The compiler will usually halt on that, but on occasion you get "lucky".
Otherwise, conflicting names or defines. Do you have anything named std for example?