在 Vector 中使用 Class 的成员函数
Using member function of Class in a Vector
编辑:您需要制作这样的向量:
向量 myVector(5);
不是这样的:
矢量 myVector[5];
而不是 vector<team> players[5];
vector<team> players(5);
必须是。通过此操作,您将创建一个包含 5 个玩家的向量。在您的代码中创建了 5 个空向量。
实际上,您应该得到更多的错误。我得到以下信息:
cl /nologo /EHsc /Za /W4 Whosebug.cpp
Whosebug.cpp
Whosebug.cpp(5) : error C2146: syntax error : missing ';' before identifier 'name'
Whosebug.cpp(5) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Whosebug.cpp(9) : error C2061: syntax error : identifier 'string'
Whosebug.cpp(10) : error C2146: syntax error : missing ';' before identifier 'getName'
Whosebug.cpp(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Whosebug.cpp(10) : warning C4183: 'getName': missing return type; assumed to be a member function returning 'int'
Whosebug.cpp(22) : error C2511: 'void team::setName(std::string)' : overloaded member function not found in 'team'
Whosebug.cpp(3) : see declaration of 'team'
Whosebug.cpp(23) : error C2065: 'name' : undeclared identifier
Whosebug.cpp(23) : error C2065: 'b' : undeclared identifier
Whosebug.cpp(30) : error C2556: 'std::string team::getName(void)' : overloaded function differs only by return type from 'int team::getName(void
Whosebug.cpp(10) : see declaration of 'team::getName'
Whosebug.cpp(30) : error C2371: 'team::getName' : redefinition; different basic types
Whosebug.cpp(10) : see declaration of 'team::getName'
Whosebug.cpp(31) : error C2065: 'name' : undeclared identifier
Whosebug.cpp(42) : error C2039: 'setRuns' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
Whosebug.cpp(43) : error C2039: 'setName' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
Whosebug.cpp(45) : error C2039: 'getName' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
Whosebug.cpp(45) : error C2039: 'getRuns' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
Class C++ 标准库的名称以 std::
为前缀。那只是他们名字的一部分。最好始终使用全名。特别是,在头文件的全局范围内使用 using namespace std;
是非常糟糕的做法。
所以让我们删除 using namespace std
并在所有地方写上 std::
。
team.h:
#include<string>
class team {
std::string name;
int runs;
public:
void setName(std::string a);
std::string getName();
void setRuns(int b);
int getRuns();
};
team.cpp:
#include<string>
#include "team.h"
void team::setRuns(int b) {
runs=b;
}
void team::setName(std::string a) {
name=b;
}
int team::getRuns() {
return runs;
}
std::string team::getName() {
return name;
}
main.cpp:
#include<iostream>
#include<vector>
#include<cstdio>
#include "team.h"
int main() {
std::vector<team> players[5];
players[0].setRuns(10);
players[0].setName("Michael");
printf("%s %d",players[0].getName(),players[0].getRuns());
system("pause");
return 0;
}
这消除了大部分错误:
Whosebug.cpp(22) : error C2065: 'b' : undeclared identifier
Whosebug.cpp(39) : error C2039: 'setRuns' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
Whosebug.cpp(40) : error C2039: 'setName' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
Whosebug.cpp(42) : error C2039: 'getName' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
Whosebug.cpp(42) : error C2039: 'getRuns' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
setRuns
中的a
肯定是打字错误。我们也会解决这个问题。我还将删除不必要的 system("pause");
。现在我们的代码 仅 您询问的错误。
让我们仔细看看下面一行:
std::vector<team> players[5]
我认为这里的误解是[5]
指定了std::vector
的大小。这是一种误解。 std::vector
没有固定大小,默认从 0 个元素开始。它不需要任何 []
语法来初始化。这里的 []
语法表示一个 数组 。数组是固定大小的元素集合。
所以您在这里创建的是一个 5 个向量的数组。显然,这根本不是你想要的。只需将 [5]
替换为 (5)
即可获得 "vector that starts with 5 elements" 的含义:
std::vector<team> players(5);
现在可以编译了。但它可能会在 运行 时 崩溃 ,因为你也错误地使用了 printf
:
printf("%s %d",players[0].getName(),players[0].getRuns());
printf
是一个 C 函数,它在 C++ 出现之前就已经设计好了。 %s
表示需要 C 风格的字符串。您可以提供这样一个:
printf("%s %d",players[0].getName().c_str(),players[0].getRuns());
或者您只使用 C++ 流:
std::cout << players[0].getName() << " " << players[0].getRuns();
编辑:您需要制作这样的向量:
向量 myVector(5);
不是这样的:
矢量 myVector[5];
而不是 vector<team> players[5];
vector<team> players(5);
必须是。通过此操作,您将创建一个包含 5 个玩家的向量。在您的代码中创建了 5 个空向量。
实际上,您应该得到更多的错误。我得到以下信息:
cl /nologo /EHsc /Za /W4 Whosebug.cpp
Whosebug.cpp
Whosebug.cpp(5) : error C2146: syntax error : missing ';' before identifier 'name'
Whosebug.cpp(5) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Whosebug.cpp(9) : error C2061: syntax error : identifier 'string'
Whosebug.cpp(10) : error C2146: syntax error : missing ';' before identifier 'getName'
Whosebug.cpp(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Whosebug.cpp(10) : warning C4183: 'getName': missing return type; assumed to be a member function returning 'int'
Whosebug.cpp(22) : error C2511: 'void team::setName(std::string)' : overloaded member function not found in 'team'
Whosebug.cpp(3) : see declaration of 'team'
Whosebug.cpp(23) : error C2065: 'name' : undeclared identifier
Whosebug.cpp(23) : error C2065: 'b' : undeclared identifier
Whosebug.cpp(30) : error C2556: 'std::string team::getName(void)' : overloaded function differs only by return type from 'int team::getName(void
Whosebug.cpp(10) : see declaration of 'team::getName'
Whosebug.cpp(30) : error C2371: 'team::getName' : redefinition; different basic types
Whosebug.cpp(10) : see declaration of 'team::getName'
Whosebug.cpp(31) : error C2065: 'name' : undeclared identifier
Whosebug.cpp(42) : error C2039: 'setRuns' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
Whosebug.cpp(43) : error C2039: 'setName' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
Whosebug.cpp(45) : error C2039: 'getName' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
Whosebug.cpp(45) : error C2039: 'getRuns' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
Class C++ 标准库的名称以 std::
为前缀。那只是他们名字的一部分。最好始终使用全名。特别是,在头文件的全局范围内使用 using namespace std;
是非常糟糕的做法。
所以让我们删除 using namespace std
并在所有地方写上 std::
。
team.h:
#include<string>
class team {
std::string name;
int runs;
public:
void setName(std::string a);
std::string getName();
void setRuns(int b);
int getRuns();
};
team.cpp:
#include<string>
#include "team.h"
void team::setRuns(int b) {
runs=b;
}
void team::setName(std::string a) {
name=b;
}
int team::getRuns() {
return runs;
}
std::string team::getName() {
return name;
}
main.cpp:
#include<iostream>
#include<vector>
#include<cstdio>
#include "team.h"
int main() {
std::vector<team> players[5];
players[0].setRuns(10);
players[0].setName("Michael");
printf("%s %d",players[0].getName(),players[0].getRuns());
system("pause");
return 0;
}
这消除了大部分错误:
Whosebug.cpp(22) : error C2065: 'b' : undeclared identifier
Whosebug.cpp(39) : error C2039: 'setRuns' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
Whosebug.cpp(40) : error C2039: 'setName' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
Whosebug.cpp(42) : error C2039: 'getName' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
Whosebug.cpp(42) : error C2039: 'getRuns' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
setRuns
中的a
肯定是打字错误。我们也会解决这个问题。我还将删除不必要的 system("pause");
。现在我们的代码 仅 您询问的错误。
让我们仔细看看下面一行:
std::vector<team> players[5]
我认为这里的误解是[5]
指定了std::vector
的大小。这是一种误解。 std::vector
没有固定大小,默认从 0 个元素开始。它不需要任何 []
语法来初始化。这里的 []
语法表示一个 数组 。数组是固定大小的元素集合。
所以您在这里创建的是一个 5 个向量的数组。显然,这根本不是你想要的。只需将 [5]
替换为 (5)
即可获得 "vector that starts with 5 elements" 的含义:
std::vector<team> players(5);
现在可以编译了。但它可能会在 运行 时 崩溃 ,因为你也错误地使用了 printf
:
printf("%s %d",players[0].getName(),players[0].getRuns());
printf
是一个 C 函数,它在 C++ 出现之前就已经设计好了。 %s
表示需要 C 风格的字符串。您可以提供这样一个:
printf("%s %d",players[0].getName().c_str(),players[0].getRuns());
或者您只使用 C++ 流:
std::cout << players[0].getName() << " " << players[0].getRuns();