2

I'm trying without any luck to include strings in my C++ Win32 API beginner project. The code won't compile if I define a string. What's going on?


Details:

I was working in Dev C++, but now have switched to Code::Blocks using the (default?) "Gnu GCC Compiler".

Here are the code cases I have tried, all similar, with their results:

Compiles successfully:

#include <windows.h>
#include <string.h>  //<string> throws "no such file or directory"

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
//...the rest works perfectly, omitted in following examples

Fails:

#include <windows.h>
#include <string.h>

// Error: "string" does not name a type
string myString;  

// ...WndProc

Compiles successfully:

#include <windows.h>
#include <string.h>
using namespace std;

// ...WndProc

Fails:

#include <windows.h>
#include <string.h>
using namespace std;

// Error: "string" does not name a type
string myString; 

// ...WndProc

Fails:

#include <windows.h>
#include <string.h>

// Error: expected constructor, destructor, or type conversion before "myString"
// Error: expected ',' or ';' before "myString"
std::string myString; 

// ...WndProc

I asked this question a few days ago but deleted it because it seemed like a dumb question. However, it wasn't solved and now has come back to haunt me. Thanks in advance.

4 Answers 4

2

Does the source file have a .cpp extension? If it's .c, it will compile as C code, which probably excludes the directories containing the standard C++ headers.

Sign up to request clarification or add additional context in comments.

4 Comments

Not with gcc. GCC will compile whatever file you throw at it in the language you tell it to. Compiling a .cpp file with 'gcc' for example will successfully compile the program but fail to link it with the standard C++ library. Switches and/or arg0 are how to specify language, using 'g++' instead of 'gcc' for example.
YES that's the problem. Changed the extension and removed the .h and it runs fine. Many thanks to you and @Noah.
Guess gcc does pay attention to extension. Just looked at man page. Can certainly be overridden though.
@Noah: The choice of compiler (g++ or gcc) is probably being made by Code::Blocks.
2

#include <string.h> //<string> throws "no such file or directory"

Something is seriously broken with either your compiler installation or your use of it. Whatever comes after this, not being able to include the header for std::string is going to make it very difficult to use one.

You can install the GCC suite without C++ support, maybe that's your problem.

1 Comment

Thanks Noah, tested again removing the .h and for some reason it ran perfectly. Still no solution but this is good for now. Will accept when timer finishes (if no better answers).
0

<string.h> contains ANSI C string macros and function declarations (see here) , not the C++ string. To use std::string, you need to do

#include <string>

(no .h)

#include <windows.h>
#include <string>

std::string myString; 

6 Comments

The OP knows that already, and pointed out that doing so fails.
Umm, no -- see his comment to @Noah's answer above: "Thanks Noah, tested again removing the .h and for some reason it ran perfectly." Yeah - the reason it ran perfectly is because it's the right thing to do.
How can this answer possibly be downvoted? It's informative and helpful. If the author of the question followed these instructions it would lead him to the next problem (that being he had a .c extension instead of .cpp). +1 from me bgporter.
I didn't downvote, but I'd imagine it was downvoted because it failed to address the fact that the OP specifically mentioned trying these things. I know that I always find it frustrating when people's answers indicate they didn't read my question. The funny thing is, when people upvote to counter downvotes you actually inflate the person's cred, rather than balancing it.
I upvoted because I thought it was relevant and helpful, not to counter the downvote.
|
0

string.h has only methods to handle the string. For example, strcpy, strlen etc... (http://opengroup.org/onlinepubs/007908799/xsh/string.h.html)

If you want to use std::string, you should use . If there is no file, check that file.

Good luck :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.