I have a problem with the following code:
class MainWindow
{
...
private:
bool CreateWindow(std::string, int, int, int, bool);
...
}
and
bool MainWindow::CreateWindow(std::string title, int width, int height,
int bits, bool fullscreen)
{
...
Visual Studio highlights the method definition with the following error:
int reateWindow(std::string title, int width, int height, int bits, bool fullscreen)
Error: class "MainWindow" has no member called "CreateWindowExW"
and the compiler outputs the following:
warning C4003: not enough actual parameters for macro 'CreateWindowW'
error C2039: 'CreateWindowExW' : is not a member of 'MainWindow'
I noticed that if I change the method name to something else, that does not begin with a capital C, the error goes away. I'm new to Windows development. What gives?
It is simply because CreateWindow is a macro created by Microsoft...
It is defined in WinUser.h.
Related
It took me a good hour to locate this issue. The following code
class Test {
public:
void method();
int _member;
};
void Test::method()
{
struct S {
int s = 0; // same with int s {0};
};
_member;
}
int main(int argc, const char* argv [])
{
return 0;
}
Produces a compilation error:
1>error C2327: 'Test::_member' : is not a type name, static, or
enumerator
1>error C2065: '_member' : undeclared identifier
And the error goes away as soon as I replace int s = 0; with int s;.
This only occurs in MSVC 2013, not 2015. I'm pretty sure it's a compiler bug but I want to make sure it's not some C++ peculiarity that I'm not familiar with (something that changed between C++11 and C++14).
[C++11: 12.6.2] defines NSDMIs in C++11, and neither this section nor any other section in the document defines such a constraint on the syntax. Therefore, it must be an implementation bug.
And, since GCC, Clang and Visual Studio 2015 all accept the code, I don't think any more detailed investigation is worthwhile.
My question relates to three files and how they relate to each other:
In one file I have a bunch of predefined types such as Uint, int32, etc.
In the other two I have a class that is used to categories exceptions (which is mostly static functions) and definitions for the class.
All of the types are in the file Types.h, with a macro which allows the types to be defined globally:
namespace Enigma {
//Omitted Types
typedef std::uint32_t Uint32;
typedef std::string string;
//Omitted Types
}
#if defined(USING_GLOBAL_TYPES)
using namespace aNamespace;
#endif
In the other files I have the following (or similar to it anyway):
Header file:
#include "Types.h"
namespace Enigma {
class ExceptionCategory {
typedef Uint32 CategoryID;
static CategoryID GetIDFromName(const string& name) noexcept;
};
}
Source file:
Engima::ExceptionCategory::CategoryID Enigma::ExceptionCategory::GetIDFromName(const string& name) noexcept {
//Omitted Code
}
Now the problem lies within the Source file according to the error messages the Compiler is throwing at me which include the following:
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2143: syntax error: missing ',' before '&'
error C2511: 'Enigma::ExceptionCategory::IDType Enigma::ExceptionCategory::GetIDFromName(const int) noexcept': overloaded member function not found in 'Enigma::ExceptionCategory'
Edit: Major Rewording
In the cpp file, remove the double colon before the namespace.
aNamespace::int32 aNamespace::aClass::aStaticFunction() noexcept {
//does a thing
}
Quite a simple mistake:
Was missing the namespace Enigma before string in the source code.
It's unusual because it has been working fine for ages until suddenly it stopped working
Engima::ExceptionCategory::CategoryID Enigma::ExceptionCategory::GetIDFromName(const Enigma::string& name) noexcept {
//Omitted Code
}
I am attempting to modify an project that was written in an old version of Visual Studio. I now use Visual Studio 2010. When I opened the project it was automatically converted to 2010 format. However, when I build it I get the following error:
Error 1 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\x\dcd\dcdchild.h 22 1
The file in question is quite small:
// dcdchild.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// CDCDChildWnd frame
class CDCDChildWnd : public CMDIChildWnd
{
DECLARE_DYNCREATE(CDCDChildWnd)
protected:
CDCDChildWnd(); // protected constructor used by dynamic creation
// Attributes
public:
// Operations
public:
// Implementation
protected:
virtual ~CDCDChildWnd();
virtual CDCDChildWnd::PreCreateWindow(CREATESTRUCT& cs);
// Generated message map functions
//{{AFX_MSG(CDCDChildWnd)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
The line in question is:
virtual CDCDChildWnd::PreCreateWindow(CREATESTRUCT& cs);
I have looked up the definition of CREATESTRUCT and it is:
typedef struct tagCREATESTRUCT {
LPVOID lpCreateParams;
HINSTANCE hInstance;
HMENU hMenu;
HWND hwndParent;
int cy;
int cx;
int y;
int x;
LONG style;
LPCTSTR lpszName;
LPCTSTR lpszClass;
DWORD dwExStyle;
} CREATESTRUCT, *LPCREATESTRUCT;
So it does indeed include variable type int. The question is, how do I resolve it?
The declaration virtual CDCDChildWnd::PreCreateWindow(CREATESTRUCT& cs); is invalid. It should look like this:
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
Your method has no return type declared:
virtual CDCDChildWnd::PreCreateWindow(CREATESTRUCT& cs);
You probably meant:
virtual BOOL CDCDChildWnd::PreCreateWindow(CREATESTRUCT& cs);
(The error message is referring to the fact that in early versions of C you were allowed to omit the return type, in which case int was assumed - this is not allowed in C++ however.)
A file I am using in my project has many declaration in this style:
static VALUE do_checksum(int, VALUE*, uLong (*)(uLong, const Bytef*, uInt));
...
static VALUE
do_checksum(argc, argv, func)
int argc;
VALUE *argv;
uLong (*func)(uLong, const Bytef*, uInt);
{
...
}
While I have never written code in this way myself, I am sure it is correct. However, my compiler returns
error: 'VALUE do_checksum' redeclared as different kind of symbol
error: 'argc' was not declared in this scope
What is wrong here?
Windows 7
Code::Blocks w/ MinGW
You have yourself some old-style C parameter list declarations.
Here's a sample fix:
static VALUE do_checksum(
int argc,
VALUE *argv,
uLong (*func)(uLong, const Bytef*, uInt)
)
{
...
}
An even better fix would be to make a type alias for func like so:
using func_ptr_type = uLong (*)(uLong, const Bytef*, uInt);
I am trying to use the function gluTessCallback but I get C2440 error. I have no idea why.
Here is the code:
#define callback void(CALLBACK*)()
template<typename T>
class Tessellation
{
private:
GLUtesselator *pTess;
void CALLBACK tessError(GLenum error)
{
sendErrorMessage((char *)gluErrorString(error), true);
}
public:
void Triangulation3D(T* & point, short numOfPoints)
{
pTess = gluNewTess();
gluTessCallback(pTess, GLU_TESS_ERROR, (callback)tessError);
}
};
The error is on gluTessCallback function:
error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'void (__stdcall *)(void)'
Why do I get this compile error?
The error id C2440 on Visual Studio is a type conversion error.
The problem in your code is that you are trying to pass a class method Tessellation::tessError() as function pointer to gluTessCallback(), which expects a pointer to a global C-style function.
A class method is very different from a free/global function and you cannot pass it as a simple function pointer because it needs an object to go along with it every time, the this pointer.
A solution to your problem would be to declare tessError() as a static method, making it effectively the same as a free/global function scoped inside the class, like so:
template<typename T>
class Tessellation
{
private:
static void CALLBACK tessError(GLenum error)
{
sendErrorMessage((char *)gluErrorString(error), true);
}
...
And pass it to gluTessCallback():
gluTessCallback(pTess, GLU_TESS_ERROR, (callback)&Tessellation<T>::tessError);
The only downside to this approach is that a static tessError() can no longer access class variables. Which doesn't seem like a problem to you, since it is not doing so right now, it appears.