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:osition();
string makeString();
string s;
};
Position:osition() {
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::perator =(const
/usr/include/g++/std/bastring.h:232: classbasic_string said:
basic_string said:/usr/include/g++/std/bastring.h:234: classbasic_string said::perator =(const char *)
basic_string said:basic_string said::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.