Class not recognising string type with gcc (?)

B

Bob Hairgrove

Hi,

I'm not very experienced with c++ so I'm sure this is
something obvious that is easily solved, but for the life
of me I can't seem to figure it out. I'd be very grateful
for some knowledgeable insight into this.

My class Position has a string (private, public - doesn't
seem to matter which), which I construct in a member
function and wish to return calls from main(). However, although
I've declared the return type as "string" the compiler (g++
2.95 and 3.4) the type does not seem to recognise this.

To illustrate the problem, I've created a small class and
main() that illustrates this problem:

==========================================================
#include <string>

class Position {

private:

int p;

public:

Position::position();
string makeString();
string s;

};

Position::position() {
p = 5;
}

string Position::makeString () {
s[0] = 'h';
s[1] = 'i';
return s;
}


main () {

Position p;
string myString;

myString = p.makeString;
}

=========================================================
In my real program makeString is a more complicated function
and the string created is based on Position's data members,
so makeString needs to be a member function.

g++ 2.95 complains:

g++ test7.cpp
test7.cpp: In function `int main()':
test7.cpp:34: no match for `string & = {unknown type}'
/usr/include/g++/std/bastring.h:166: candidates are: class
basic_string said:
basic_string said:
::eek:perator =(const
basic_string said:
/usr/include/g++/std/bastring.h:232: class
basic_string said:
basic_string said:
::eek:perator =(const char *)
/usr/include/g++/std/bastring.h:234: class
basic_string said:
basic_string said:
::eek:perator =(char)

g++34 test7.cpp
test7.cpp:8: error: `string' does not name a type
test7.cpp:13: error: `string' does not name a type
test7.cpp:22: error: `string' does not name a type
test7.cpp: In function `int main()':
test7.cpp:32: error: `string' undeclared (first use this function)
test7.cpp:32: error: (Each undeclared identifier is reported only once
for each function it appears in.)
test7.cpp:32: error: expected `;' before "myString"
test7.cpp:34: error: `myString' undeclared (first use this function)
test7.cpp:34: error: 'class Position' has no member named 'makeString'

If someone knows the problem, I'd appreciate a hint. I've
googled for this and looked a Stroustrup without joy. I'm probably
doing something hilariously stupid :)

TIA
DCH

You need to write std::string, not just string. The STL classes are in
the namespace std.
 
V

Victor Bazarov

David Carter-Hitchin said:
I'm not very experienced with c++ so I'm sure this is
something obvious that is easily solved, but for the life
of me I can't seem to figure it out. I'd be very grateful
for some knowledgeable insight into this.

Get a more recent book on C++. You need to learn about namespaces
and especially about the 'std' namespace.
[...]
==========================================================
#include <string>

class Position {

private:

int p;

public:

Position::position();
string makeString();

Make it

std::string makeString();

here.
string s;

And here:

std::string s;

And everywhere else.

Victor
 
R

Robert Bauck Hamar

Hi,
My class Position has a string (private, public - doesn't
seem to matter which), which I construct in a member
function and wish to return calls from main(). However, although
I've declared the return type as "string" the compiler (g++
2.95 and 3.4) the type does not seem to recognise this.

It's not string, it's std::string
To illustrate the problem, I've created a small class and
main() that illustrates this problem:

==========================================================
#include <string>

class Position {

private:

int p;

public:

Position::position();
string makeString();

std::string makeString();
string s;

std::string s;
};

Position::position() {
p = 5;
}

Why not:
Position::position() : p(5) {}
?
string Position::makeString () {

std::string Position::makeString() {

Note: by the time you come here, s is empty. That means that s[0] and
s[1] doesn't exist. On a non-const string, this is undefined
behaviour. Hint: use s.append('h') instead.
s[0] = 'h';
s[1] = 'i';
return s;
}


main () {

Position p;
string myString;

myString = p.makeString;
}

=========================================================
In my real program makeString is a more complicated function
and the string created is based on Position's data members,
so makeString needs to be a member function.
 
P

Petec

Victor said:
Get a more recent book on C++. You need to learn about namespaces
and especially about the 'std' namespace.

He said he was using Stroustrup's. It omits the using directives, and just
uses unqualified names. I think it mentions it somewhere in the
introduction.

- Pete

<snip>
 
D

David Carter-Hitchin

Hi,

I'm not very experienced with c++ so I'm sure this is
something obvious that is easily solved, but for the life
of me I can't seem to figure it out. I'd be very grateful
for some knowledgeable insight into this.

My class Position has a string (private, public - doesn't
seem to matter which), which I construct in a member
function and wish to return calls from main(). However, although
I've declared the return type as "string" the compiler (g++
2.95 and 3.4) the type does not seem to recognise this.

To illustrate the problem, I've created a small class and
main() that illustrates this problem:

==========================================================
#include <string>

class Position {

private:

int p;

public:

Position::position();
string makeString();
string s;

};

Position::position() {
p = 5;
}

string Position::makeString () {
s[0] = 'h';
s[1] = 'i';
return s;
}


main () {

Position p;
string myString;

myString = p.makeString;
}

=========================================================
In my real program makeString is a more complicated function
and the string created is based on Position's data members,
so makeString needs to be a member function.

g++ 2.95 complains:

g++ test7.cpp
test7.cpp: In function `int main()':
test7.cpp:34: no match for `string & = {unknown type}'
/usr/include/g++/std/bastring.h:166: candidates are: class
basic_string said:
basic_string said:
::eek:perator =(const
basic_string said:
/usr/include/g++/std/bastring.h:232: class
basic_string said:
basic_string said:
::eek:perator =(const char *)
/usr/include/g++/std/bastring.h:234: class
basic_string said:
basic_string said:
::eek:perator =(char)

g++34 test7.cpp
test7.cpp:8: error: `string' does not name a type
test7.cpp:13: error: `string' does not name a type
test7.cpp:22: error: `string' does not name a type
test7.cpp: In function `int main()':
test7.cpp:32: error: `string' undeclared (first use this function)
test7.cpp:32: error: (Each undeclared identifier is reported only once
for each function it appears in.)
test7.cpp:32: error: expected `;' before "myString"
test7.cpp:34: error: `myString' undeclared (first use this function)
test7.cpp:34: error: 'class Position' has no member named 'makeString'

If someone knows the problem, I'd appreciate a hint. I've
googled for this and looked a Stroustrup without joy. I'm probably
doing something hilariously stupid :)

TIA
DCH
 
P

Peter Koch Larsen

David Carter-Hitchin said:

Hi David
I'm not very experienced with c++ so I'm sure this is
something obvious that is easily solved, but for the life
of me I can't seem to figure it out. I'd be very grateful
for some knowledgeable insight into this.

My class Position has a string (private, public - doesn't
seem to matter which), which I construct in a member
function and wish to return calls from main(). However, although
I've declared the return type as "string" the compiler (g++
2.95 and 3.4) the type does not seem to recognise this.

As mentioned elsewhere, "modern" C++ uses namespaces. You should use
std::string instead of string (or using namespace std - or using
std::string).
To illustrate the problem, I've created a small class and
main() that illustrates this problem:

==========================================================
#include <string>

class Position {

private:

int p;

public:

Position::position();
string makeString();
string s;

};

Position::position() {
p = 5;
}

string Position::makeString () {
s[0] = 'h';
s[1] = 'i';

It is not safe to index s here. Instead use s.push_back (or - if s might
already be initialized - s.reserve(2)). This assures that the memory you
touch is valid.
return s;
}


main () {

Position p;
string myString;

myString = p.makeString;
myString = p.makeString();
You are forgetting parantesis - assigning a memberfunction to a string.
[snip]

Kind regards
Peter
 
R

Robert Bauck Hamar

David Carter-Hitchin said:
string Position::makeString () {
s[0] = 'h';
s[1] = 'i';

It is not safe to index s here. Instead use s.push_back (or - if s might

I can't find any push_back(), but g++ compiles. Is there a push_back()?
already be initialized - s.reserve(2)). This assures that the memory you
touch is valid.

The string::reserve() member function does not change the size of the
string. Writing s if i >= s.length() invokes undefined behaviour
(Except if s is a const string, then s.[s.length()] returns char()).
There is a member function, s.at(i), that is safer than s.

Either call string::resize(n), or use the s.append(c) or s+=c!
 
R

Raveen.B

hi,
just change the string assignment into the following

s = "hi";

this has solves the problem, also a small suggestion, try using the
references when trying to return classes and structs unless u need a
copy... else references are the best,
___
Regards
Ravs
 
P

Peter Koch Larsen

Robert Bauck Hamar said:
David Carter-Hitchin said:
string Position::makeString () {
s[0] = 'h';
s[1] = 'i';

It is not safe to index s here. Instead use s.push_back (or - if s might

I can't find any push_back(), but g++ compiles. Is there a push_back()?
Yes.
already be initialized - s.reserve(2)). This assures that the memory you
touch is valid.

The string::reserve() member function does not change the size of the
string. Writing s if i >= s.length() invokes undefined behaviour
(Except if s is a const string, then s.[s.length()] returns char()).

Are you sure of that?
There is a member function, s.at(i), that is safer than s.

Either call string::resize(n), or use the s.append(c) or s+=c!

You are correct... this code becomes convoluted anyway if the string is not
known to be empty. The cleanest approach probably is to use an empty string
and then use push_back, eventually assigning the new string to the result.
 
D

David Carter-Hitchin

Peter said:
myString = p.makeString();
You are forgetting parantesis - assigning a memberfunction to a string.

Hi Peter (and every other kind soul that took the trouble to
write back.)

First of all can I say a big thank you to everyone for all their
suggestions - I'm bowled over by the first rate support you've
offered a pesky amateur like me.

Secondly, the real problem was the one above - missing parens
(essentially a syntax error in my opinion, but unfortunately
not in the 'eyes' of the compiler :) Perhaps something like
Flexelint would have picked this up... (if only I could afford
it :)

The stuff about namespaces was relevant of course in my rather
duff pseudo code (thanks to all who pointed out missing
initializers, default arguments etc. - not absent in my real
program :) My real code had the "using namespace std" in there
so this led me to a character-by-character comparison of an
example piece of string code which I know works by Tom Swan
which *doesn't* have "using namespace std" - this somehow made
me realise about the missing parens. Interestingly Mr Swan says
that most code will work without "using namespace std" providing
none of the symbols clash. His book (GNU C++ for Linux) was
written presumably at a time (c1999) when gcc was taking on
namespaces as he says his efforts to compile anything using them
wouldn't work.

I have the latest Stroutrup (3rd edition) and although he mentions
namespaces he doesn't (IMHO) make a great deal over them. p46ff does
mention "using namespace std" to avoid the "std::" prefix though.

Anyway - I got my code compiled and debugged and it found a solution
to a little problem a collegue set me. When I'm sure of the
results I'll knock a webpage together in case anyone is interested -
I think it's a really nice programming challenge. In the course
of doing this I learnt how to use <string>, <map> and <deque> and
was extremely impressed by the performance :)

MTA
DCH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top