在Vector中使用Class的成员函数

时间:2015-03-07 10:13:00

标签: c++ class oop vector

编辑:您需要制作这样的矢量:

vector myVector(5);

不喜欢这样:

vector myVector [5];

2 个答案:

答案 0 :(得分:2)

而不是vector<team> players[5]; vector<team> players(5);。通过此操作,您将创建一个包含5个玩家的向量。在你的代码中,创建了5个空向量。

答案 1 :(得分:0)

实际上,您应该会遇到更多错误。我得到以下内容:

cl /nologo /EHsc /Za /W4 stackoverflow.cpp
stackoverflow.cpp
stackoverflow.cpp(5) : error C2146: syntax error : missing ';' before identifier 'name'
stackoverflow.cpp(5) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
stackoverflow.cpp(9) : error C2061: syntax error : identifier 'string'
stackoverflow.cpp(10) : error C2146: syntax error : missing ';' before identifier 'getName'
stackoverflow.cpp(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
stackoverflow.cpp(10) : warning C4183: 'getName': missing return type; assumed to be a member function returning 'int'
stackoverflow.cpp(22) : error C2511: 'void team::setName(std::string)' : overloaded member function not found in 'team'
        stackoverflow.cpp(3) : see declaration of 'team'
stackoverflow.cpp(23) : error C2065: 'name' : undeclared identifier
stackoverflow.cpp(23) : error C2065: 'b' : undeclared identifier
stackoverflow.cpp(30) : error C2556: 'std::string team::getName(void)' : overloaded function differs only by return type from 'int team::getName(void
        stackoverflow.cpp(10) : see declaration of 'team::getName'
stackoverflow.cpp(30) : error C2371: 'team::getName' : redefinition; different basic types
        stackoverflow.cpp(10) : see declaration of 'team::getName'
stackoverflow.cpp(31) : error C2065: 'name' : undeclared identifier
stackoverflow.cpp(42) : error C2039: 'setRuns' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
        with
        [
            _Ty=team
        ]
stackoverflow.cpp(43) : error C2039: 'setName' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
        with
        [
            _Ty=team
        ]
stackoverflow.cpp(45) : error C2039: 'getName' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
        with
        [
            _Ty=team
        ]
stackoverflow.cpp(45) : error C2039: 'getRuns' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
        with
        [
            _Ty=team
        ]

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;
}

这会删除大多数错误:

stackoverflow.cpp(22) : error C2065: 'b' : undeclared identifier
stackoverflow.cpp(39) : error C2039: 'setRuns' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
        with
        [
            _Ty=team
        ]
stackoverflow.cpp(40) : error C2039: 'setName' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
        with
        [
            _Ty=team
        ]
stackoverflow.cpp(42) : error C2039: 'getName' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
        with
        [
            _Ty=team
        ]
stackoverflow.cpp(42) : error C2039: 'getRuns' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
        with
        [
            _Ty=team
        ]

a中的setRuns肯定是一个错字。我们也会解决这个问题。我还会删除不必要的system("pause");。现在我们的代码只显示 您询问的错误。

让我们仔细看看以下一行:

std::vector<team> players[5]

我认为这里的误解是[5]指定了std::vector的大小。这是一种误解。 std::vector没有固定大小,默认情况下将从0个元素开始。初始化不需要任何[]语法。这里的[]语法表示数组。数组是固定大小的元素集合。

所以你在这里创建的是一个包含5个向量的 数组 。显然,这根本不是你想要的。只需将[5]替换为(5)即可获得&#34;含有5个元素的矢量&#34;的含义:

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();