LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-03-2007, 03:52 PM   #1
aatwell
Member
 
Registered: Apr 2007
Location: San Jose, CA
Posts: 31

Rep: Reputation: 15
incomplete type error using cpp with gcc


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;
}
 
Old 11-03-2007, 04:13 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by aatwell View Post
I'm compiling the following program with the command:
gcc -o test.exe test.cpp

Any clues would be greatly appreciated.
Clue: gcc compiles C programs, g++ compiles c++ programs.
 
Old 11-03-2007, 04:50 PM   #3
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Also, in C++ programs when using C headers, one usually prefixes the name with a 'c' and drops the '.h', so for example <stdio.h> becomes <cstdio>.
 
Old 11-03-2007, 05:28 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
<string.h> is a C header which should not include std::string. You need the C++ header, which is <string>.
ta0kira
 
Old 11-03-2007, 06:10 PM   #5
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
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.
 
Old 11-03-2007, 07:15 PM   #6
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by Nylex View Post
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>.
 
Old 11-03-2007, 11:51 PM   #7
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
This should work:
Code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main()
{
        std::string value = "Hello World\n";
        cout << value;
        return 0;
}
Quote:
g++ -o test test.cpp

Last edited by paulsm4; 11-03-2007 at 11:53 PM.
 
Old 11-04-2007, 02:20 AM   #8
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by osor View Post
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).
 
Old 11-04-2007, 11:12 AM   #9
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by Nylex View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
tai64nlocal.c:55: error: dereferencing pointer to incomplete type ExCIA Linux - General 1 03-31-2009 09:49 AM
error: field has incomplete type halturata Programming 4 04-11-2008 05:01 PM
error: dereferencing pointer to incomplete type ChullDouvre Programming 2 05-02-2007 12:16 AM
Error: dereferencing pointer to incomplete type cynthia_thomas Programming 1 05-01-2006 08:10 AM
sem has incomplete type error in building the filesystem image sudhirvemana Linux - Newbie 1 11-25-2004 01:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration