gpt4 book ai didi

c++ - 需要检查访问列表、映射、结构和其他容器的语法

转载 作者:行者123 更新时间:2023-11-28 06:20:23 25 4
gpt4 key购买 nike

这是我第一个 C++ 程序的一部分,我非常不确定我使用的语法。寻找改进代码的修复和建议。

这是我的结构,包含 3 个映射:

typedef struct {    
std::map<std::string, int> firstCounts;
std::map<std::string, int> pairCounts;
std::map<std::string, std::list<std::string>> follows;
} LanguageModel;

这些是需要使用此 LanguageModel 结构的函数:

using namespace std;    

string buildPair(string s1, string s2) {
string s3;
s3 = s1 + " " + s2;
return s3;
}

LanguageModel countSequence(LanguageModel &model, vector<string> &Seq) {
//append "<start>" and "<end>" to the vector of strings
vector<string>::iterator v = Seq.begin();
Seq.push_front("<START>");
v = Seq.end();
Seq.push_end("<END">);
v = Seq.begin();

//create word pairs
vector<string> pairs;
for (int n = 0; n <= Seq.size(); n++) {
pairs.at(n) = buildPair(Seq[n], Seq[n+1]);
}

//feed each word to map 1 with number of times it was seen
for (int m = 0; m <= Seq.size(); m++) {
++model.firstCounts[Seq[m]];
model.firstCounts[Seq[m]] = count(Seq.begin(), Seq.end(), Seq[m]);
}


//feed each word pair and the number of times it was seen
for (int k = 0; k <= Seq.size(); k++) {
++model.pairCounts[Seq[k]];
model.pairCounts[Seq[k]] = count(Seq.begin(), Seq.end(), Seq[m]);
}

//feed each unique first word in a word pair and the second words in the pairs
for (int l = 0; l <= Seq.size(); l++) {
istringstream iss(pairs[l]);
string sub;
iss >> sub;
string sub2;
iss >> sub2;

if (Seq[l] = sub) {
++model.follows[sub];
model.follows[sub].push_back(sub2);
}

}

return model;
}


string genNext(LanguageModel &model2, string &testWord, int Number) {

//use results of countSequence

return model2.follows[testWord][Number];

}

当然,代码还不能编译。

这些是我得到的错误:

LangModel.h:28:1: error: ‘string’ does not name a type
string buildPair(std::string s1, std::string s2);
^
LangModel.h:30:63: error: ‘string’ was not declared in this scope
LanguageModel countSequence(LanguageModel &model, std::vector<string> &Seq);
^
LangModel.h:30:63: note: suggested alternative:
In file included from /usr/include/c++/4.8/iosfwd:39:0,
from /usr/include/c++/4.8/ios:38,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from LangModel.h:13,
from hw5.cpp:10:
/usr/include/c++/4.8/bits/stringfwd.h:62:33: note: ‘std::string’
typedef basic_string<char> string;
^
In file included from hw5.cpp:10:0:
LangModel.h:30:69: error: template argument 1 is invalid
LanguageModel countSequence(LanguageModel &model, std::vector<string> &Seq);
^
LangModel.h:30:69: error: template argument 2 is invalid
LangModel.h:32:1: error: ‘string’ does not name a type
string genNext(LanguageModel &model2, std::string &testWord, int Number);
^
hw5.cpp: In function ‘int main()’:
hw5.cpp:34:1: error: expected ‘}’ at end of input

更新:修复了上面的错误。现在得到这些:

LangModel.cpp: In function ‘LanguageModel countSequence(LanguageModel&, std::vector<std::basic_string<char> >&)’:
LangModel.cpp:37:6: error: ‘class std::vector<std::basic_string<char> >’ has no member named ‘push_front’
Seq.push_front("<START>");
^
LangModel.cpp:39:6: error: ‘class std::vector<std::basic_string<char> >’ has no member named ‘push_end’
Seq.push_end("<END">);
^
LangModel.cpp:39:22: error: expected primary-expression before ‘)’ token
Seq.push_end("<END">);
^
LangModel.cpp:44:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int n = 0; n <= Seq.size(); n++) {
^
LangModel.cpp:49:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int m = 0; m <= Seq.size(); m++) {
^
LangModel.cpp:55:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int k = 0; k <= Seq.size(); k++) {
^
LangModel.cpp:57:67: error: ‘m’ was not declared in this scope
model.pairCounts[Seq[k]] = count(Seq.begin(), Seq.end(), Seq[m]);
^
LangModel.cpp:61:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int l = 0; l <= Seq.size(); l++) {
^
LangModel.cpp:62:20: error: variable ‘std::istringstream iss’ has initializer but incomplete type
istringstream iss(pairs[l]);
^
LangModel.cpp:68:25: error: could not convert ‘(&(& Seq)->std::vector<_Tp, _Alloc>::operator[]<std::basic_string<char>, std::allocator<std::basic_string<char> > >(((std::vector<std::basic_string<char> >::size_type)l)))->std::basic_string<_CharT, _Traits, _Alloc>::operator=<char, std::char_traits<char>, std::allocator<char> >((*(const std::basic_string<char>*)(& sub)))’ from ‘std::basic_string<char>’ to ‘bool’
if (Seq[l] = sub) {
^
LangModel.cpp:69:10: error: no match for ‘operator++’ (operand type is ‘std::map<std::basic_string<char>, std::list<std::basic_string<char> > >::mapped_type {aka std::list<std::basic_string<char> >}’)
++model.follows[sub];
^
LangModel.cpp: In function ‘std::string genNext(LanguageModel&, std::string&, int)’:
LangModel.cpp:80:33: error: no match for ‘operator[]’ (operand types are ‘std::map<std::basic_string<char>, std::list<std::basic_string<char> > >::mapped_type {aka std::list<std::basic_string<char> >}’ and ‘int’)
return model2.follows[testWord][Number];
^
LangModel.cpp:81:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^

修正了上面的大部分错误。这些是剩下的。我确定这些都是语法相关的,但不确定如何修复它们。

LangModel.cpp:69:25: error: could not convert ‘(&(& Seq)->std::vector<_Tp, _Alloc>::operator[]<std::basic_string<char>, std::allocator<std::basic_string<char> > >(((std::vector<std::basic_string<char> >::size_type)l)))->std::basic_string<_CharT, _Traits, _Alloc>::operator=<char, std::char_traits<char>, std::allocator<char> >((*(const std::basic_string<char>*)(& sub)))’ from ‘std::basic_string<char>’ to ‘bool’
if (Seq[l] = sub) {
^
LangModel.cpp:70:10: error: no match for ‘operator++’ (operand type is ‘std::map<std::basic_string<char>, std::list<std::basic_string<char> > >::mapped_type {aka std::list<std::basic_string<char> >}’)
++model.follows[sub];
^
LangModel.cpp: In function ‘std::string genNext(LanguageModel&, std::string&, int)’:
LangModel.cpp:81:33: error: no match for ‘operator[]’ (operand types are ‘std::map<std::basic_string<char>, std::list<std::basic_string<char> > >::mapped_type {aka std::list<std::basic_string<char> >}’ and ‘int’)
return model2.follows[testWord][Number];

最佳答案

您遇到的第一个错误是:

LangModel.h:28:1: error: ‘string’ does not name a type
string buildPair(std::string s1, std::string s2);
^

这意味着编译器不知道string。您需要在程序顶部添加:

#include <string>

我看到您也有 using namespace std;。虽然这适用于小程序,请参阅 Why is “using namespace std;” considered bad practice? .最好删除 using namespace std; 并使用 std::string 而不仅仅是 string

关于c++ - 需要检查访问列表、映射、结构和其他容器的语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29403812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com