使用gcc(?)识别字符串类型的类 [英] Class not recognising string type with gcc (?)
问题描述
我对c ++不是很有经验所以我确定这是很明显很容易解决的问题,但是很明显对于生活
我b $ b我似乎无法弄明白。我将非常感激
对此有一些了解。
我的班级职位有一个字符串(私人,公共 - 不
似乎很重要,我在一个成员构建
函数并希望从main()返回调用。但是,虽然我已经将返回类型声明为string编译器(g ++
2.95和3.4)类型似乎没有认识到这一点。
为了说明问题,我创建了一个小类和
main()说明了这个问题:
===================== ============================= ========
#include< string> ;
班级职位{
私人:
int p;
public:
Position :: Position();
string makeString();
string s;
};
职位::职位(){
p = 5;
}
string Position :: makeString(){
s [0] =''h'';
s [1] =''我';
返回s;
}
main(){
>
位置p;
string myString;
myString = p.makeString;
}
========================================== ======== =======
在我的真实程序中,makeString是一个更复杂的函数
,创建的字符串基于Position的数据成员,
所以makeString需要是一个成员函数。
g ++ 2.95抱怨:
g ++ test7.cpp
test7.cpp:函数`int main ()'':
test7.cpp:34:不匹配`string& = {unknown type}''
/usr/include/g++/std/bastring.h:166:候选人是:class
basic_string< char,string_char_traits< char> ,__ defau lt_alloc_template< false,0>
&
basic_string< char,string_char_traits< char>,__ defau lt_alloc_template< false,0> :: operator =(const
basic_string< char,string_char_traits< char>,__ defau lt_alloc_template< false,0>&)
/usr/include/g++/std/bastring.h:232:class
basic_string< char,string_char_traits< char>,__ defau lt_alloc_template< false,0> &安培;
basic_string< char,string_char_traits< char>,__ defau lt_alloc_template< false,0> :: operator =(const char *)
/usr/include/g++/std/bastring.h:234:class, br />
basic_string< char,string_char_traits< char>,__ defau lt_alloc_template< false,0> &安培;
basic_string< char,string_char_traits< char>,__ defau lt_alloc_template< false,0> :: operator =(char)
g ++ 34 test7.cpp
test7.cpp:8:错误:`string''没有命名类型
test7.cpp:13:错误:`string''没有命名类型
test7.cpp:22:错误:`string''没有命名类型
test7.cpp:在函数`int main()''中:
test7.cpp:32:错误:`string''未声明(首先使用此函数)
test7.cpp:32:错误:(每个未声明的标识符仅报告一次
。)
test7.cpp:32:错误:预期`;''在'myString'之前'
test7。 cpp:34:错误:`myString''未声明(首先使用此函数)
test7.cpp:34:错误:''class Position''没有名为''makeString''的成员/>
如果有人知道这个问题,我会很感激。我已经用Google搜索了这个并且看起来没有喜悦的Stroustrup。我很可能
做一些搞笑的傻事:-)
TIA
DCH
" David Carter-Hitchin" <哒*** @ carter-hitchin.clara.co.uk>写了...我对c ++不是很有经验所以我确定这是显而易见的,很容易解决的,但对于我的生活来说我似乎无法弄明白。我将非常感谢您对此有一些了解。
获取一本关于C ++的最新书。您需要了解名称空间
,特别是关于''std''命名空间。
[...]
======== ========================================== ========
#include< string>
班级职位{
私人:
int p;
public:
Position :: Position();
string makeString();
制作
std :: string makeString();
here。
string s;
这里:
std :: string s;
以及其他任何地方。
};
[...]
Victor
>
在文章< 10 **************** @ doris.uk.clara.net>中,David Carter-Hitchin写道:你好,
我的班级职位有一个字符串(私人,公共 - 似乎并不重要),我在一个成员
函数中构建并希望从main()返回调用。但是,虽然我已经将返回类型声明为string编译器(g ++
2.95和3.4)类型似乎没有认识到这一点。
这不是字符串,它是std :: string
为了说明问题,我创建了一个小班并且
main()说明了这个问题:
=============================== ================== =========
#include< string>
班级职位{
int p;
公开:
位置:: Position();
字符串makeString ();
std :: string makeString();
string s;
std :: string s;
};
位置::位置(){
p = 5 ;
}
为什么不:
位置:: position():p(5){}
?
string Position :: makeString(){
std :: string Position :: makeString(){
注意:当你来到这里时,s是空的。这意味着s [0]和
s [1]不存在。在非const字符串上,这是未定义的
行为。提示:改为使用s.append(''h'')。
s [0] =''h'';
s [1] =''i'';
返回s;
}
main(){
位置p;
字符串myString;
myString = p.makeString;
}
================================= ================ ========
在我的真实程序中,makeString是一个更复杂的功能
并且创建的字符串是基于Position的的数据成员,
所以makeString需要成为一个成员函数。
-
Robert Bauck Hamar
blockquote>
Victor Bazarov写道:" David Carter-Hitchin" <哒*** @ carter-hitchin.clara.co.uk>写了...我对c ++不是很有经验所以我确定这是显而易见的,很容易解决的,但对于我的生活来说我似乎无法弄明白。我非常感谢你对这方面的一些了解。
获取一本关于C ++的最新书。你需要了解名称空间
,特别是关于''std''命名空间。
他说他正在使用Stroustrup''。它省略了using指令,只是
使用不合格的名称。我认为它在
介绍中提到它。
- Pete
< snip>
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<char,string_char_traits<char>,__defau lt_alloc_template<false,0>& basic_string<char,string_char_traits<char>,__defau lt_alloc_template<false,0>::operator =(const basic_string<char,string_char_traits<char>,__defau lt_alloc_template<false,0> &) /usr/include/g++/std/bastring.h:232: class
basic_string<char,string_char_traits<char>,__defau lt_alloc_template<false,0> & basic_string<char,string_char_traits<char>,__defau lt_alloc_template<false,0>::operator =(const char *) /usr/include/g++/std/bastring.h:234: class
basic_string<char,string_char_traits<char>,__defau lt_alloc_template<false,0> & basic_string<char,string_char_traits<char>,__defau lt_alloc_template<false,0>::operator =(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
解决方案"David Carter-Hitchin" <da***@carter-hitchin.clara.co.uk> wrote...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
In article <10****************@doris.uk.clara.net>, David Carter-Hitchin wrote: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.
--
Robert Bauck Hamar
Victor Bazarov wrote:"David Carter-Hitchin" <da***@carter-hitchin.clara.co.uk> wrote...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.
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>
这篇关于使用gcc(?)识别字符串类型的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!