Use of string

Feb 13, 2009 at 11:52am
Hi all,
I am learning C++ using a book by Bruce Eckel, Thinking in C++. I have hit a problem when solving an exercise at the end of one of the chapters which left me a bit confused.
I need to create a struct which has three string objects as its members. This is my code for the header file:

#include <string>

struct TestStruct
{
string a, b, c;
};


I also have the implementation .cpp file for it.
I included the header file on the file where I am using it but the compiler comes up with the error "string does not define a type".
I can see what he sort of means, as string is not a native type, but I can not find a way of solving it.
Is there a problem with my struct declaration?

Thanks.
Best Regards
Luis

Feb 13, 2009 at 12:22pm
I don't know for sure, but u might try including STRING.H.
Feb 13, 2009 at 12:27pm
closed account (z05DSL3A)
1
2
3
4
5
6
#include <string>

struct TestStruct
{
     std::string a, b, c;
};
Feb 13, 2009 at 12:36pm
Hi Grey Wolf,
I have tried that and it did not work. This is the error given now: "using-declaration for non-member at class scope"
best Regards
Luis
Feb 13, 2009 at 12:39pm
Not sure but also try this :P

using namespace std;
Feb 13, 2009 at 12:54pm
closed account (z05DSL3A)
Luis, What compiler/IDE/platform are you using?

You also say you "have the implementation .cpp file for it", there is no need for any implementation other than the code you posted.

Can you post your code? using code tags ie:
[code]
your header code...
[/code]
[code]
your imp code...
[/code]


Last edited on Feb 13, 2009 at 12:58pm
Feb 13, 2009 at 1:18pm
Hi Grey Wolf,
this is my code:

[TestStruct.h]
//TestStruct.h
#include <string>

struct TestStruct
{
string a, b, c;
};
[/TestStruct.h]

[TestStruct.cpp]
//TestStruct.cpp
#include "TestStruct.h"

TestStruct.a = "";
TestStruct.b = "";
TestStruct.c = "";
[/TestStruct.cpp]

[CH5Exe2.cpp]
//Ch5Exe2.cpp
// usage
#include <cstdlib>
#include<iostream>
#include "TestStruct.h"

using namespace std;

int main(int argc, char *argv[])
{
TestStruct x;
x.a = "2";
x.b = "3";
x.c = "4";

cout<< x.a << endl;
cout<< x.a << endl;
cout<< x.a << endl;

system("pause");
return EXIT_SUCCESS;
}

[/CH5Exe2.cpp]

Thanks
Luis







Feb 13, 2009 at 1:31pm
closed account (z05DSL3A)
The problem is TestStruct.cpp, you do not need it. If you comment out the three line it should work.


1
2
3
4
5
6
7
//TestStruct.h
include <string>

struct TestStruct
{
     std::string a, b, c;
};


1
2
3
4
5
6
//TestStruct.cpp
#include "TestStruct.h"

//TestStruct.a = "";
//TestStruct.b = "";
//TestStruct.c = ""; 


PS, what I meant by use code tags, was put the actual text [code] above, you code and [/code] below it.

if you type in

[code]
//TestStruct.cpp
#include "TestStruct.h"

//TestStruct.a = "";
//TestStruct.b = "";
//TestStruct.c = "";
[/code]

it is displayed as

1
2
3
4
5
6
//TestStruct.cpp
#include "TestStruct.h"

//TestStruct.a = "";
//TestStruct.b = "";
//TestStruct.c = ""; 


edit:corrected spelling mistakes
Last edited on Feb 13, 2009 at 1:38pm
Feb 13, 2009 at 1:32pm
Hi,
The IDE I am using is DEVC++/MinGW on Windows.
Regards
Luis
Feb 13, 2009 at 1:48pm
Hi,
The original error as gone but I have a new one:
"using-declaration for non-member at class scope"
Best Regards
Luis
Feb 13, 2009 at 1:58pm
closed account (z05DSL3A)
I do not use DevC++ but would expect there to be more information than just "using-declaration for non-member at class scope".

somthing like:
TestStruct.cpp:4: using-declaration for non-member at class scope
can you post it please.
Feb 13, 2009 at 2:17pm
there is nothing else there except for:
"using-declaration for non-member at class scope
expect ';' before "a" "
Feb 13, 2009 at 2:53pm
closed account (z05DSL3A)
Okay, as I can't tell where that is having a problem try this:

1) remove the TestStruct.cpp file altogether from your project.
2) make sure the two files you have left look like this:

1
2
3
4
5
6
7
//TestStruct.h
#include<string>

struct TestStruct
{
     std::string a, b, c;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Ch5Exe2.cpp
// usage
#include<iostream>
#include "TestStruct.h"

using namespace std;

int main(int argc, char *argv[])
{
    TestStruct x;
    x.a = "2";
    x.b = "3";
    x.c = "4";

    cout<< x.a << endl;
    cout<< x.a << endl; 
    cout<< x.a << endl;

    system("pause");
    return EXIT_SUCCESS;
}


3) Find the clean or rebuild build option and perform a fresh build.

NB: You have cout<< x.a << endl; three times in your main, you may have wanted to have one for x.a, x.b, and x.c but that is insadental.
Feb 13, 2009 at 10:12pm
Hi,
I see what you mean, and it probably will work, but I would like to know why using the .cpp file does not work.

The other thing is that on this book the author says that using the "using"directive(I presume that "std::string a, b, c;" is a sort of using directive) in a .h file was not desirable.

thanks for your help
Luis
Feb 13, 2009 at 10:30pm
closed account (z05DSL3A)
As I said before; there is nothing in your header file that needs implementing, the empty .cpp file could possibly have confused the compiler.

The following file uses the "using" directive
1
2
3
4
5
6
7
8
//TestStruct.h
#include<string>
using namespace std;

struct TestStruct
{
     string a, b, c;
};

It is not desirable to do this because it would bring all items in the std namespace into what ever scope the header is included in.

Full scope resolution (ie namespace::item) is what you should do in headers.
Feb 15, 2009 at 7:09pm
Hi Grey Wolf,
thank you for your help, it did work.
As I am just starting, I am trying to get a programming standard, but obviously it does not work for everything.
Best Regards
Luis
Feb 15, 2009 at 9:52pm
I guess the error is too obvious to see it, eh?

1
2
3
4
5
6
//TestStruct.cpp
#include "TestStruct.h"

TestStruct.a = "";
TestStruct.b = "";
TestStruct.c = "";


TestStruct is a structure and you never declared a variable. You have to do it this way:

1
2
3
4
5
6
7
8
//TestStruct.cpp
#include "TestStruct.h"

TestStruc mystruc;

mystruc.a = "";
mystruc.b = "";
mystruc.c = "";
Feb 15, 2009 at 10:23pm
closed account (z05DSL3A)
I guess the error is too obvious to see it, eh?

No, not if you think that was the error.
Topic archived. No new replies allowed.