Can somebody tell me why I get this error?

Ok, so I am doing a directX tutorial thing and it gave me this code:

// directX.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

LPCTSTR helloworld = "Hello World!";


int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}

#include <windows.h> // include the basic windows header file

// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd)
{
// create a "Hello World" message box using MessageBox()
MessageBox(NULL,
L"Hello World!",
L"Just another Hello World program!",
MB_ICONEXCLAMATION | MB_OK);

// return 0 to Windows
return 0;
}

Which should apparently create a window.. However, it gives me this error:


1>------ Build started: Project: directX, Configuration: Debug Win32 ------
1> directX.cpp
1>c:\users\sharon\documents\visual studio 2010\projects\directx\directx\directx.cpp(6): error C2146: syntax error : missing ';' before identifier 'helloworld'
1>c:\users\sharon\documents\visual studio 2010\projects\directx\directx\directx.cpp(6): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\sharon\documents\visual studio 2010\projects\directx\directx\directx.cpp(6): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\sharon\documents\visual studio 2010\projects\directx\directx\directx.cpp(6): error C2440: 'initializing' : cannot convert from 'const char [13]' to 'int'
1> There is no context in which this conversion is possible
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\winnt.h(447): error C2378: 'LPCTSTR' : redefinition; symbol cannot be overloaded with a typedef
1> c:\users\sharon\documents\visual studio 2010\projects\directx\directx\directx.cpp(6) : see declaration of 'LPCTSTR'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I am using Microsoft Visual C++ 2010 Express... Why does it not work?
Wow, lots of mistakes here. What a low-quality tutorial.

1. The LPCTSTR type defined in the Windows API headers. Therefore, you must #include <windows.h> BEFORE using it.
2. The LPCTSTR data type requires that the string literal be enclosed by the _T() or TEXT() macros, like this:

LPCTSTR helloworld = _T("Hello World!");

3. It is strange that the sample includes both the entry point for a console application and the entry point of a GUI application. I don't think this is technically an error, so I won't comment any further about it.
4. The GUI entry point WinMain() calls MessageBox() using Unicode string literals. WRONG!!! It must be changed to MessageBoxW(), or the string literals MUST be enclosed by the _T() or TEXT() macros. The same goes for all of the Windows API functions that this tutorial of yours will incorrectly use.
5. This tutorial declares a string constant but it never uses it. Instead, it uses its value inside the function directly. So why define the constant in the first place? And don't be confused: Declaring the constant was a good thing; the bad thing was not using it.
Last edited on
Topic archived. No new replies allowed.