我将向量作为参数传递给类函数时遇到了一些问题。类在另一个文件中。这是我的代码:
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
请帮忙! 谢谢
答案 0 :(得分:1)
class1.h不包含<string>
或<vector>.
在“<string>
包含class1.h
(不是你应该依赖的东西!)之前,你恰好在你的源文件中包含vector
这一事实”,你已经“忘掉了”前一个遗漏。
在后一种情况下,class1.cpp根本不知道z[i].childNodes[1].childNodes[3].childNodes[1].childNodes[1].childNodes[0].onclick=function(){
document.getElementsByClassName('cq-proceed')[0].style="display:block !important;";
var q = document.getElementsByClassName('hide_me');
for(var r = 0; r < q.length; r++){
q[r].style="display:none;";
}
}
的含义。
修复您的包含。
答案 1 :(得分:-1)
您的问题不在于将参数传递给类。 以下代码适用于此:
{{1}}
正如Orbit中Lightness Races之前提到的,你的问题不包括向class1的vector和string