字符串向量不传递到类功能
String vectors not passing to a class function
我在传递向量,作为参数到类函数时遇到一些问题。类在另一个文件中。这是我的代码:
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中的功能时。错误是:
- 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...documentsprograms and gamestessttesstclass1.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...documentsprograms and gamestessttesstclass1.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...documentsprograms and gamestessttesstclass1.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...documentsprograms and gamestessttesstclass1.h 6
- Severity Code Description Project File Line Suppression State
Error C2065 'string': undeclared identifier tesst c:users...documentsprograms and gamestessttesstclass1.h 6
- Severity Code Description Project File Line Suppression State
Error C2065 'string': undeclared identifier tesst c:users...documentsprograms and gamestessttesstclass1.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...documentsprograms and gamestessttesstmain.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...documentsprograms and gamestessttesstclass1.cpp 9
请帮忙!谢谢
class1.h不包括 <string>
或 <vector>.
您是在包含class1.h
之前恰好将<string>
包含在源文件中的事实,这是前者的遗漏(不是您应该依靠的!)。
在后一种情况下,class1.cpp根本不知道 vector
的含义。
修复您的包含。
您的问题不是将参数传递给类。以下代码在这里工作:
#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;
}
如前所述,轨道上的轻度竞赛,您的问题不包括向量和字符串到Class1
相关文章:
- C++模板功能检查唯一向量
- 分割错误:向量中的擦除功能
- 迭代向量以获得搜索功能
- 基于不同字段的对象向量的排序功能
- 如何使用指针向量为班级制作复制功能
- 如何在不复制的情况下将向量向量从类函数传递到功能
- 将功能参数添加到向量
- 使用ENABLE_IF和SFINAE时,功能参数类型扣除(std容器,例如向量)失败
- 向量擦除功能无法正常工作
- (C )创建可以从功能访问的动态全局数组/向量
- STD功能替换向量中的字节范围
- 定义功能,以便它可以接受列表或向量
- 如何管理STD ::功能的STD ::向量
- 当程序输入主要功能时,为什么要清除此std ::向量
- 有没有办法将向量从辅助函数传递到C 中的另一个辅助功能
- 从功能返回时,向量的深拷贝行为
- 初始化功能指针的向量
- 字符串向量不传递到类功能
- 如何声明功能向量(lambdas)
- 如何创建<wchar_t>具有擦除(size_t pos)方法的功能向量?