I got a strange compilation error when I followed the MSDN document to use CA2W to convert big5 strings to unicode strings in Visual Studio 2005.
This is the code I wrote:
#include <string>
#include <atldef.h>
#include <atlconv.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string chineseInBig5 = "\xA4\xA4\xA4\xE5";
ATL::CA2W(chineseInBig5.c_str());
return 0;
}
The compilation error: error C3861: 'AtlThrowLastWin32': identifier not found
I don't know how this could happen. The document of AtlThrowLastWin32 shows that atldef.h is required, but I couldn't find the declaration of AtlThrowLastWin32 in atldef.h.
I finally solved this problem by adding 2 include headers:
#include <atlbase.h>
#include <atlstr.h>
I don't know why the MSDN document doesn't mention that.
Related
I'm writing an MFC application on Visual Studio 2015 in C++. I added some code which uses members of std library and suppose to take an int and create from it a hex char* with the prefix "0x". I tried to build the project on VS 2015 and VS 2017 from two different computers and I get the same errors - VS doesn't recognize the std library. I've tied running the code on other programs (Clion) and it worked well.
When I include #include <stdlib> I get the following error:
cannot open source file "stdlib"
I've tried re-installing VS, and checked I have all the necessary extensions to support C++, but I guess there's still something missing. How can I fix it?
The code:
std::ostringstream ss;
int i = 7;
ss << std::hex << std::showbase << i;
std::string str = ss.str();
const char *output = str.c_str();
std::cout << output << std::endl;
and included the following headers:
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <strstream>
I get the following errors:
'Ostringstream': is not a member of 'std'
'Ostringstream': undeclared identifier
'ss': undeclared identifier
'hex': is not a member of 'std'
'showbase': is not a member of 'std'
'string': is not a member of 'std'
'string': undeclared identifier
Thank you.
I've included the headers in the wrong order. In every C++ project in Visual Studio it includes "stdafx.h" library automatically. This library contains many of the commonly used libraries such as <string> and etc. The solution was to write the includes in the following way:
#include "stdafx.h"
// other headers of the form "header.h"
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <strstream>
// other headers of the form <header>
instead of:
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <strstream>
// other headers of the form <header>
#include "stdafx.h"
// other headers of the form "header.h"
a bit more about this in this question
Thanks for everyone who tried to help, I appreciate your time and attention.
I have two files: DateTime.h and DateTime.cpp which are shown below:
DateTime.h
class DateTime
{
public:
static string getCurrentTimeStamp();
};
DateTime.cpp
#include "stdafx.h"
#include "DateTime.h"
#include <ctime>
#include <chrono>
#include <iostream>
#include <string>
using namespace std;
string DateTime::getCurrentTimeStamp()
{
return "";
}
My compiler (Visual Studio 2012) is spitting out errors the moment I have the function getCurrentTimeStamp() return an std::string object. The errors all point to syntactic problems but none are clear. Does anyone understand why this may be the case?
Update: Here are (some of) the errors.
Error 6 error C2064: term does not evaluate to a function taking 0
arguments c:\users\anthony\documents\code\consoleapplication1\datetime.cpp 21 1 ConsoleApplication1
Error 1 error C2146: syntax error : missing ';' before identifier
'getCurrentTimeStamp' c:\users\anthony\documents\code\consoleapplication1\datetime.h 5 1 ConsoleApplication1
Error 7 error C2146: syntax error : missing ';' before identifier
'getCurrentTimeStamp' c:\users\anthony\documents\code\consoleapplication1\datetime.h 5 1 ConsoleApplication1
Error 5 error C2371: 'DateTime::getCurrentTimeStamp' : redefinition;
different basic
types c:\users\anthony\documents\code\consoleapplication1\datetime.cpp 10 1 ConsoleApplication1
When trying to diagnose an issue with a header file, especially a simple one like this, step 1 is to try and see what the compiler sees.
#include is a pre-processor directive, so the compiler doesn't see it, instead the compiler sees the pre-processed output of the file that you're trying to include.
So your code would look something like this:
#include "stdafx.h"
//#include "DateTime.h"
class DateTime
{
public:
static string getCurrentTimeStamp();
};
//#include "DateTime.h"
#include <ctime>
#include <chrono>
#include <iostream>
#include <string>
using namespace std;
string DateTime::getCurrentTimeStamp()
{
return "";
}
http://rextester.com/MODV66772
When I try and compile this on RexTester's online Visual Studio, I get very different errors, telling me that your stdafx.h isn't empty.
If I modify the code a little:
//#include "stdafx.h"
//#include "DateTime.h"
#include <string>
class DateTime
{
public:
static std::string getCurrentTimeStamp();
};
//#include "DateTime.h"
#include <ctime>
#include <chrono>
#include <iostream>
#include <string>
using namespace std;
string DateTime::getCurrentTimeStamp()
{
return "";
}
This now compiles without the errors/warnings you are reporting: http://rextester.com/PXE62490
The changes:
include in the header file, since the header depends on it,
use std::string instead of string,
The C++ compiler is a single-pass compiler, so the header file can't know that you are intending to do using namespace std later, and even if it did, it's a terrible practice because the std namespace is densely populated.
If you absolutely can't do with typing std:: all over the place, try using the names you need, e.g.
using std::string; // string no-longer needs to be std::string
My code is as follows
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include <windows.h>
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
BYTE* pAlloc1 = NULL;
return 0;
}
creating following errors.
error C2065: 'BYTE' : undeclared identifier
What am I doing wrong here?
You have #include "stdafx.h", which usually means that you're using a precompiled header. If you use a precompiled header, anything preceding the precompiled header will be discarded.
Try reordering your #include lines so that "stdafx.h" is first. (Or change stdafx.h to #include <windows.h>, which is generally where you want to put commonly-used system headers.)
I just started c++
here is the code for a basic main declaration and based on many tutorails I have found, also contains the code to print hello world to console
// TestApp.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "Hello World";
}
I am using VS 2012 Express, I dont know which compiler but the "cout" is underlined in red
errors are as follows:
error C2039 'cout': is nit a member of 'std' ln 9 col 1 error
C2065 : undeclared identifier ln 9 col 1 IntelliSense:
namespace "std" has no member "cout" ln 9 col 7
I do not understand why it is giving an error, could someone please enlighten me?
The error is telling you that std::cout hasn't yet been declared anywhere in this translation unit. You can't use something if it hasn't been declared. std::cout is declared in the C++ standard library <iostream> header:
#include <iostream>
If you receive a similar error in the future and you need to know which header to include, look up some documentation for the particular function/type you want to use. For example, if you look at cppreference.com, it states "Defined in header <iostream>".
Include below code :
#include <iostream>
Try this:
#include "stdafx.h"
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "Hello World";
}
should be fine now
When I compile my project I get this error.
C:\src\libs\nvrtpaudio\FileRtpSource.
cpp(61) : error C3861: 'timeBeginPeriod': identifier not found
C:\src\libs\nvrtpaudio\FileRtpSource.
cpp(71) : error C3861: 'timeEndPeriod': identifier not found
gmake[5]: *** [_out/win7_x86_debug/FileRtpSource.obj] Error 2
I included windows.h but this error still persists. Anyone know how to resolve this?
MSDN says:
Header: Mmsystem.h (include Windows.h)
So you are expected to include "windows.h" and be fine, but what MSDN does not say is that this assumes you don't have WIN32_LEAN_AND_MEAN defined, which - when defined, and this also can be the case with project created from template - exlcudes "mmsystem.h" you need.
So you have to either make sure you don't have WIN32_LEAN_AND_MEAN in your project, or otherwise include directly:
#include "stdafx.h"
#include <mmsystem.h> // <<--- Here we go
#pragma comment(lib, "winmm.lib")
int _tmain(int argc, _TCHAR* argv[])
{
timeBeginPeriod(0);
return 0;
}