ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Greetings,
I have an issue when compiling a program in cpp. The specific error is:
test.cpp: In function "int main()" :
test.cpp:11: error: aggregate "std::string value" has incomplete type and cannot be defined
I'm using Fedora Core 7 with kernel-2.6.21-1.3194.fc7 and I'm using gcc-4.1.2-27.fc7. I'm compiling the following program with the command:
gcc -o test.exe test.cpp
Any clues would be greatly appreciated.
Code:
#include <stdio.h>
#include <vector>
#include <string.h>
using namespace std;
int main()
{
printf("Hello World!\n");
std::string value;
value = "Hello World";
return 0;
}
For that simple program though, you don't need to include <string>. If you were using some function like substr() or something, then you'd need it.
…huh? I agree the program is simple enough for lines 9 and 10 to be omitted (since they don’t do anything). But since line 9 is how it is, you need to #include <string>.
It compiles and runs fine without "#include <string>" (on my machine with gcc 4.1.2, anyway).
First and foremost, even if it does, it’s wrong. There is nothing special about strings that allows their use without their definition, and their is nothing special about namespace std (in fact, namespace std does not exist without the standard library headers). See §17.4.2.1.
Second, are you sure? Whose gcc do you have? For me, it throws an error with (and most of these are near-vanilla) g++-3.4.6, g++-4.1.2, and g++-4.2.2.
Also, there is a separate issue (probably) not present in the original post. When you #include certain standard headers, they end up including others. This behavior is expressly permitted by the standard (§17.4.4.1). The only problem is it is not specified what headers include other headers except that a header must include another header if it contains a needed definition. So free rein is given to implementors on what headers can include what other headers. For example, with most g++ implementations, if you #include <iostream>, you have implicitly #included <string> as well. Except for the unlikely event that your <vector> header #includes <string>, I don’t see how your implementation successfully compiles the original program. This gets to limit the portability of compiling a program, since some implementors chose differing headers to be included by other headers. As such, the next version of g++ (4.3 branch) will strive to eliminate all unnecessary headers inclusion of other headers. If that happens, a program which successfully compiles with g++ will be guaranteed (at least in that respect) to compile on any other conforming compiler.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.