我试图读取一个文本文件“dictionary.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(一个Word*
)的指针,这不是你想要的。 C ++不是Java,其中一切都是指向某事物的隐式指针。在这里,您可以拥有自动对象(Word)和指向对象的指针(Word *)。
从类设计的角度来看,你创建了一个Word,它应该将三个字符串word
,definition
和type
保持在一起。好。什么是字典?该名称表明它是单词的容器,因此向量应该是Dictionary的属性,而不是填充的参数。否则,名称应该是DictionaryLoader或那些行中的某些内容。
所以我首先修复Word类。为了使事情更简单,我建议你把所有东西都公开,所以我将使用struct
instad of class
。在Google C++ Style Guide之后,我在成员变量名后添加了一个下划线。由于不需要初始化,我会避免它。相反,您将从流中加载单词,因此有一个加载单词的方法可能是个好主意。运营商会更好,但让我们留待未来。你阅读的方式不允许包括空格在内的定义。所以我冒昧地使用getline
来使用引用的字符串(里面没有引号!)。
这是一个例子dictionary.txt
(你应该包含在你的问题中!记住Minimal, Complete, and Verifiable example):
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桌面向导”创建项目,不要为解决方案创建目录,并在以下对话框中选择“清空项目”。
如果将所有代码都推送到单个文件,那么只需创建一个Dictionary对象并调用函数loadDictionary()
字典d; d.loadDictionary(字);
代码需要完全修改。但是一些非常明显的快速修复: