-3

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.

user14092875
  • 145
  • 1
  • 1
  • 12

1 Answers1

1

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.

user14092875
  • 145
  • 1
  • 1
  • 12
  • I am glad you have got your solution and thanks for your sharing, I would appreciate it if you mark them as answer and this will be beneficial to other community. – Barrnet Chou Aug 26 '20 at 07:40
  • @BarrnetChou I will make sure to mark it as an answer as soon as I will be able to. unfortunately, for some reason (perhaps because I'm new) I can mark it as an answer only 24 hours after I've published it. – user14092875 Aug 26 '20 at 08:31