视觉 获取尝试将向量传递给类的函数C++的错误
visual Getting errors trying to pass a vector to a class's function C++
我试图读取一个文本文件"字典.txt",其中包含一些单词及其定义和类型。每个单词都意味着加载到具有定义和类型的 Word 类对象中,然后该对象旨在被推送到其他 Word 对象的矢量数组中。
但是我收到错误:
E0147 declaration is incompatible with "void Dictionary::loadDictionary(std::vector<<error-type> std::allocator<<error-type>>> &vect)" (declared at line 27)
和
E0020 identifier "loadDictionary" is undefined.
总的来说,我对 C++ 和 OOP 很陌生,所以希望对这些错误有所帮助。
谢谢堆!
法典:
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Dictionary
{
public:
void loadDictionary(vector<Word>& vect);
private:
Word w1;
string word;
string def;
string type;
};
void Dictionary::loadDictionary(vector<Word>& vect)
{
ifstream dicFile;
dicFile.open("dictionary.txt");
if (!dicFile)
{
cout << "File not found!" << endl;
exit(1);
}
int count1 = 0;
while (!dicFile.eof())
{
w1 = new Word;
dicFile >> word;
dicFile >> def;
dicFile >> type;
w1.word->word;
w1.def->def;
w1.type->type;
vect.push_back(w1);
}
}
class Word
{
public:
private:
string word;
string definition;
string type;
};
Word::Word() {
word = "";
definition = "";
type = "";
}
int main()
{
Dictionary d;
vector<Word> word;
d.loadDictionary(word);
return 0;
}
这里有一组建议,可以让一切正常并开始思考 以更 OOP 的方式解决问题(但这可能是主观的)。
正如其他人指出的,主要问题是w1
是一个词,你试图做
w1 = new Word;
这是没有意义的,因为new Word
创建了一个指向单词(Word*
的指针), 这不是你想要的。C++不是Java,其中一切都是隐式的 指向某物的指针。在这里你可以有自动对象(Word)和指针 到对象 (字*)。
从类设计的角度来看,你创建了一个应该将三者结合在一起的词 字符串word
、definition
和type
。还行。什么是字典?顾名思义 它是单词的容器,因此向量应该是字典的属性, 而不是填充的参数。否则,名称应该是 字典加载器或这些行中的内容。
所以我会从修复 Word 类开始。为了使事情更简单,我建议您拥有 所有公开的内容,所以我将使用struct
instad ofclass
.以后 谷歌C++风格指南I 在成员变量名称后添加了下划线。由于不需要初始化, 我会避免它。相反,您将从流中加载单词,因此这可能是一个好主意 具有加载单词的方法。操作员会更好,但让我们 留给未来。 您阅读的方式不允许包含空格的定义。所以我冒昧地使用getline
来使用带引号的字符串(里面没有引号!
这是一个示例dictionary.txt
(您应该包含在您的问题中!请记住最小、完整和可验证的示例):
sovereign "a king or queen" noun
desk "a type of table that you can work at, often one with drawers" noun
build "to make something by putting bricks or other materials together" verb
nice "pleasant, enjoyable, or satisfactory" adjective
代码在这里。
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
struct Word {
std::string word_;
std::string definition_;
std::string type_;
std::istream& read(std::istream& is) {
is >> word_;
std::string skip;
std::getline(is, skip, '"');
std::getline(is, definition_, '"');
is >> type_;
return is;
}
};
现在是字典。字典是单词的容器,所以我们的字典应该 里面有一个词的向量。字典中的所有变量 真的不在正确的地方。您将它们用作临时人员,因此它们应该 已放置在您的函数内。
struct Dictionary {
std::vector<Word> vect_;
bool load(const std::string& filename) {
std::ifstream is("dictionary.txt");
if (!is)
return false;
while (true) {
// Read
Word w;
w.read(is);
// Check
if (!is)
break;
// Use
vect_.push_back(w);
}
/* Alternative
Word w;
while (w.read(is)) { // Read & Check
// Use
vect_.push_back(w);
}*/
/* Another alternative
for (Word w; w.read(is);) { // Read & Check
// Use
vect_.push_back(w);
}*/
return true;
}
};
int main()
{
Dictionary d;
if (d.load("dictionary.txt"))
return EXIT_SUCCESS;
else
return EXIT_FAILURE;
}
检查Dictionary::load
功能。规则很简单:读取、检查、使用。我的建议是始终从三个注释的无限循环开始。然后添加相关代码进行读取,然后进行检查,最后使用您刚刚读取的内容。然后寻找更紧凑的替代品,如果你真的需要它们。
啊,我刚刚想起:既然你正在使用VisualStudio,那就帮自己一个忙:不要使用预编译的头。你不知道它们是什么,相信我,你在很长一段时间内都不需要它们。因此,使用"Windows 桌面向导"创建项目,不要为解决方案创建目录,并在以下对话框中选择"空项目"。
如果你把所有代码推送到单个文件,那么只需创建一个字典的对象并调用函数loadDictionary()
字典 d; d.loadDictionary(word);
代码需要完全修改。但是一些非常明显的快速修复:
- "new"运算符返回指向对象的指针。在代码中,将指针存储到类型为"Word"的非指针变量中。
- 在"while"循环中,您尝试访问另一个"Word"中的"Word"对象,然后在"loadDictionary"函数中访问类的私有变量。
- 两个类之间的数据是重复的。
- 您可以使用更简单的"结构"代替"Word"类,但是是否正确使用类并不重要。
- "error: no matching function for call to"构造函数错误
- 为什么类中的ostringstream类型的成员会导致";调用隐含删除复制构造函数";错误
- 尝试将unique_ptrs推送到向量时使用纯虚拟函数错误
- C++ OpenCV 卡尔曼滤波器构造函数错误
- C++:用户定义的显式类型转换函数错误
- C++ 合并字符串以'system'函数错误
- C++ wmain 函数错误时使用 Unicode
- 结构数组的构造函数错误,错误消息:没有构造函数实例与参数列表匹配
- C++ 中常量属性的初始化构造函数错误
- 线程 std::调用未知类型,无法专门化函数错误
- 表单显示对话框函数错误,并且不执行下面的语句
- 函数错误 C2059:语法错误:'>'不起作用
- STL向量上出现奇怪的复制构造函数错误
- C++ Visual Studio 重载函数错误:没有重载函数的实例与指定的类型匹配
- C++引用已删除函数错误
- 为什么通过带有文字编号的引用调用会出现"无匹配函数"错误?
- C++ 乘法定义的构造函数错误消息似乎错误
- 在 c++ 代码中将数组传递给函数错误
- FFMPEG avcodec_decode_video2函数错误代码
- 为什么我会收到转换函数错误