【问题标题】:String vectors not passing to a class function未传递给类函数的字符串向量
【发布时间】:2017-04-18 08:53:49
【问题描述】:

我在将向量作为参数传递给类函数时遇到了一些问题。类在另一个文件中。这是我的代码:

main.cpp:

#include <iostream>
#include <vector>
#include <string>

#include "class1.h"

using namespace std;

int main() {

    vector<string> names;

    names.push_back("Bob");
    names.push_back("Gorge");
    names.push_back("Bill");
    names.push_back("Freddy");
    names.push_back("Daniel");
    names.push_back("Emily");


    class1 obj;
    obj.printVector(names);

    system("pause");
}

class1.h:

#pragma once

class class1
{
public:
    void printVector(std::vector<string>& names);
};

class1.cpp:

#include <string>

#include "class1.h"

using namespace std;

void class1::printVector(vector<string>& names) {

    for (unsigned int i = 0; i < names.size(); i++)
    {
        cout << names[i] << endl;
    }
}

我尝试对其他数据类型(int、char、floats...)做同样的事情,它奏效了。另外,当我将它们传递给 main.cpp 中的函数时,它可以工作。错误是:

- Severity  Code    Description Project File    Line    Suppression State
Error   C3203   'allocator': unspecialized class template can't be used as a template argument for template parameter '_Alloc', expected a real type    tesst   c:\users\...\documents\programs and games\tesst\tesst\class1.h  6

- Severity  Code    Description Project File    Line    Suppression State
Error   C3203   'allocator': unspecialized class template can't be used as a template argument for template parameter '_Alloc', expected a real type    tesst   c:\users\...\documents\programs and games\tesst\tesst\class1.h  6

- Severity  Code    Description Project File    Line    Suppression State
Error   C2923   'std::vector': 'string' is not a valid template type argument for parameter '_Ty'   tesst   c:\users\...\documents\programs and games\tesst\tesst\class1.h  6   

- Severity  Code    Description Project File    Line    Suppression State
Error   C2923   'std::vector': 'string' is not a valid template type argument for parameter '_Ty'   tesst   c:\users\...\documents\programs and games\tesst\tesst\class1.h  6   

- Severity  Code    Description Project File    Line    Suppression State
Error   C2065   'string': undeclared identifier tesst   c:\users\...\documents\programs and games\tesst\tesst\class1.h  6   

- Severity  Code    Description Project File    Line    Suppression State
Error   C2065   'string': undeclared identifier tesst   c:\users\...\documents\programs and games\tesst\tesst\class1.h  6   

- Severity  Code    Description Project File    Line    Suppression State
Error   C2664   'void class1::printVector(std::vector &)': cannot convert argument 1 from 'std::vector<std::string,std::allocator<_Ty>>' to 'std::vector &' tesst   c:\users\...\documents\programs and games\tesst\tesst\main.cpp  22  

- Severity  Code    Description Project File    Line    Suppression State
Error   C2511   'void class1::printVector(std::vector<std::string,std::allocator<_Ty>> &)': overloaded member function not found in 'class1'    tesst   c:\users\...\documents\programs and games\tesst\tesst\class1.cpp    9   

请帮忙! 谢谢你

【问题讨论】:

    标签: c++ string vector parameters parameter-passing


    【解决方案1】:

    class1.h 不包括&lt;string&gt;&lt;vector&gt;.

    在包含 class1.h 之前,您恰好在源文件中包含 &lt;string&gt;(这不是您应该依赖的东西!),因此您“摆脱了”前一个遗漏。

    在后一种情况下,class1.cpp 根本不知道vector 是什么意思。

    修复您的包含。

    【讨论】:

    • 不错!在提交我的之前我没有看到你的答案。这是更准确的答案:)
    • 非常感谢您的帮助。
    • 欢迎来到 StackOverflow @jose200。您应该投票并接受此答案,因为它对您有用:)
    【解决方案2】:

    您的问题不在于将参数传递给类。 以下代码在这里工作:

    #include <iostream>
    #include <vector>
    #include <string>
    
    using namespace std;
    
    class class1 {
    public:
        void printVector(std::vector<string>& names);
    
    };
    
    void class1::printVector(vector<string>& names) {
    
        for (unsigned int i = 0; i < names.size(); i++)
        {
            cout << names[i] << endl;
        }
    }
    
    int main() {
    
        vector<string> names;
    
        names.push_back("Bob");
        names.push_back("Gorge");
        names.push_back("Bill");
        names.push_back("Freddy");
        names.push_back("Daniel");
        names.push_back("Emily");
    
    
        class1 obj;
        obj.printVector(names);
            return 0;
    }
    

    正如之前在 Orbit 中的 Lightness Races 所提到的,您的问题不在于 class1 的向量和字符串

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多