[英]Errors with vector and struct
So I decided to try and make the program I was writing in my previous question only using the vector class and I am still getting errors. 所以我决定尝试使用vector类来制作我在前一个问题中编写的程序,但我仍然遇到错误。
The Errors: 错误:
error: 'match' does not name a type
error: 'match' was not declared in the scope
error: 'conversion from 'int' to non-scalar type 'WordInfo' requested
My Code: 我的代码:
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct WordInfo {
string text;
int count;
};
int main(int argc, char** argv) {
enum {
total, unique, individual
} mode = total;
for (int c; (c = getopt(argc, argv, "tui")) != -1;) {
switch (c) {
case 't': mode = total;
break;
case 'u': mode = unique;
break;
case 'i': mode = individual;
break;
}
}
argc -= optind;
argv += optind;
string word;
vector<string> list;
vector<WordInfo> words;
int count = 0;
while (cin >> word) {
switch(mode){
case total :
count += 1;
break;
case unique :
if (find(list.begin(), list.end(), word) != list.end()){
} else {
count += 1;
}
break;
case individual :
if (find(list.begin(), list.end(), word) != list.end()) {
auto match = find(list.begin(), list.end(), word);
words.at(match - list.begin()).count++;
} else {
int count = 1;
WordInfo tmp = (word, i);
words.push_back(tmp);
}
}
}
switch (mode) {
case total: cout << "Total " << count << endl;
break;
case unique: cout << "Unique " << count << endl;
break;
case individual:
for (int i = 0; i < words.size(); i++){
cout << words.at(i); << endl;
}
break;
}
return 0;
}
Any and all help will be greatly appreciated, thank you. 非常感谢任何和所有的帮助,谢谢。
Are you sure you're using c++11? 你确定你在使用c ++ 11吗?
auto
has a completely different meaning to earlier compilers. auto
与早期的编译器有着完全不同的含义。
If you're using g++
, try compiling with the -std=c++11
flag: 如果您正在使用g++
,请尝试使用-std=c++11
标志进行编译:
g++ -std=c++11 foobar.cpp
if you're using g++, set -std=c++11 to make use of auto to let the compiler figure out the type based on the expression. 如果您正在使用g ++,请设置-std = c ++ 11以使用auto让编译器根据表达式计算出类型。
however, you might also want to clean up your code - you shouldn't have to find once in the if statement and then again inside. 但是,您可能还想清理代码 - 您不必在if语句中找到一次,然后再在里面找到。
Got some syntax error: 有一些语法错误:
test.cpp:54:44: error: use of undeclared identifier 'i'
WordInfo tmp = (word, i);
^
test.cpp:68:31: error: invalid operands to binary expression ('ostream' (aka
'basic_ostream<char>') and 'WordInfo')
cout << words.at(i); << endl;
^
See, you use undefined variable i and an extra ; 看,你使用未定义的变量i和额外的; in cout statement. 在cout声明中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.