编辑:我在字符串声明中使用std::
前缀减少了大部分错误。但是,一些函数似乎存在问题,特别是那些以字符串作为参数的函数的定义。
编辑#2:更新了我的代码(我的DID在我的函数声明中有std::
前缀,但在我的帖子中没有反映出来。在问题函数中为字符串参数添加std::
前缀时,请查看底部显示的错误。
我有一个头文件movie.h
,其中包含以下代码(相关代码):
#include <string>
class Movie
{
public:
void addMovieName(std::string movie);
void addLastName(std::string nameLast);
void addFirstName(std::string nameFirst);
private:
string movieName,
directorLastName,
directorFirstName,
directorFullName;
};
这样的实施文件movie.cpp
(相关代码):
#include "movie.h"
// addFirstName, addLastName, and addMovie name all do the same things
// so I'm only including one since they all generate the same error
void Movie::addFirstName(string nameFirst)
{
directorFirstName = nameFirst.resize(10, ' ');
}
编译后,我收到以下错误:
g++ -c movie.cpp -o movie.o
movie.cpp:225: error: variable or field ‘addFirstName’ declared void
movie.cpp:225: error: ‘int Movie::addFirstName’ is not a static member of ‘class Movie’
movie.cpp:225: error: ‘string’ was not declared in this scope
movie.cpp:226: error: expected ‘,’ or ‘;’ before ‘{’ token
movie.cpp:240: error: variable or field ‘addLastName’ declared void
movie.cpp:240: error: ‘int Movie::addLastName’ is not a static member of ‘class Movie’
movie.cpp:240: error: ‘string’ was not declared in this scope
movie.cpp:241: error: expected ‘,’ or ‘;’ before ‘{’ token
movie.cpp:255: error: variable or field ‘addMovieName’ declared void
movie.cpp:255: error: ‘int Movie::addMovieName’ is not a static member of ‘class Movie’
movie.cpp:255: error: ‘string’ was not declared in this scope
movie.cpp:256: error: expected ‘,’ or ‘;’ before ‘{’ token
make: *** [movie.o] Error 1
有些人说我需要在函数定义中将std::
添加到字符串参数中。
这样做:
// adding std:: prefix
void Movie::addFirstName(std::string nameFirst)
{
directorFirstName = nameFirst.resize(10, ' ');
}
我收到以下错误。请注意,我只更改了一个功能。我不明白的第一个错误,而其余的仍然和以前一样。
g++ -c movie.cpp -o movie.o
movie.cpp: In member function ‘void Movie::addFirstName(std::string)’:
movie.cpp:227: error: no match for ‘operator=’ in ‘((Movie*)this)->Movie::directorFirstName = nameFirst.std::basic_string<_CharT, _Traits, _Alloc>::resize [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](10u, 32)’
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/basic_string.h:485: note: candidates are: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/basic_string.h:493: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/basic_string.h:504: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
movie.cpp: At global scope:
movie.cpp:240: error: variable or field ‘addLastName’ declared void
movie.cpp:240: error: ‘int Movie::addLastName’ is not a static member of ‘class Movie’
movie.cpp:240: error: ‘string’ was not declared in this scope
movie.cpp:241: error: expected ‘,’ or ‘;’ before ‘{’ token
movie.cpp:255: error: variable or field ‘addMovieName’ declared void
movie.cpp:255: error: ‘int Movie::addMovieName’ is not a static member of ‘class Movie’
movie.cpp:255: error: ‘string’ was not declared in this scope
movie.cpp:256: error: expected ‘,’ or ‘;’ before ‘{’ token
make: *** [movie.o] Error 1
答案 0 :(得分:2)
您没有为string指定正确的命名空间,string是std命名空间的成员。在这种情况下,您需要编写std :: string,因为我看不到“using namespace std;”在你的代码中。
取代:
string movieName,
directorLastName,
directorFirstName,
directorFullName;
使用:
std::string movieName,
directorLastName,
directorFirstName,
directorFullName;
答案 1 :(得分:0)
为什么所有私有字段指针?将纯int
和float
s存储为字段是完全可以的。而且,您不需要在构造函数中“初始化”字符串。如果不这样做,它们的默认构造函数将自动调用(string
s将为空)。为什么要填充字符串?即使你在显示时需要它,也可以在那里填充它们。
答案 2 :(得分:0)
还没有人回答这个问题,所以我想我自己把它弄清楚就把它放在这里。基本上,我的解决方案是:
在标头和实施文件中使用using std::string
。
其次,声明:
stringOne = stringTwo.resize(some_number, ' ');
...失败,因为resize()
是一个void返回函数。用两个单独的陈述代替它;即:
stringOne = stringTwo;
stringOne.resize(/* blah */);
...解决了其余的错误。感谢@Jesse在评论中提到这一点。