[英]C++ Array pointer-to-object error
I am having what seems to be a common issue however reading through the replies to the similar questions I can't find the solution to my issue at all as I have already done what they are suggesting such as making the variable an array. 我有一个似乎是一个常见的问题,但是通过对类似问题的回复,我找不到我的问题的解决方案,因为我已经完成了他们的建议,例如使变量成为一个数组。 I have the following code:
我有以下代码:
#include "stdafx.h"
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
#include <algorithm>
#include <future>
using namespace std;
string eng2Str[4] = { "money", "politics", "RT", "#"};
int resArr[4];
int main()
{
engine2(eng2Str[4], resArr[4]);
system("Pause");
system("cls");
return 0;
}
void engine2(string &eng2Str, int &resArr)
{
ifstream fin;
fin.open("sampleTweets.csv");
int fcount = 0;
string line;
for (int i = 0; i < 4; i++) {
while (getline(fin, line)) {
if (line.find(eng2Str[i]) != string::npos) {
++fcount;
}
}
resArr[i] = fcount;
}
fin.close();
return;
}
Before you mark as duplicate I have made sure of the following: 在您标记为重复之前,我已确认以下内容:
The error is: 错误是:
expression must have pointer-to-object type
表达式必须具有指向对象的类型
The error is occurring at the "resArr[i] = fcount;" 错误发生在“resArr [i] = fcount;” line and am not sure why as resArr is an int array and I am trying to assign it a value from another int variable.
line并且我不确定为什么resArr是一个int数组,我试图从另一个int变量赋值。 I am quite new to C++ so any help would be great as I am really stuck!
我对C ++很陌生,所以任何帮助都会很棒,因为我真的被卡住了!
Thanks! 谢谢!
The problem is that you've declared your function to take a reference to a single string
and int
, not arrays. 问题是你已声明你的函数引用单个
string
和int
,而不是数组。 It should be: 它应该是:
void engine2(string *eng2Str, int *resArr)
or: 要么:
void engine2(string eng2Str[], int resArr[])
Then when you call it, you can give the array names as arguments: 然后,当您调用它时,您可以将数组名称作为参数:
engine2(eng2Str, resArr);
Another problem is the while
loop in the function. 另一个问题是函数中的
while
循环。 This will read the entire file during the first iteration of the for()
loop. 这将在
for()
循环的第一次迭代期间读取整个文件。 Other iterations will not have anything to read, since it will be at the end of the file already. 其他迭代将无法读取任何内容,因为它已经在文件的末尾。 You could seek back to the beginning of the file, but a better way would be to rearrange the two loops so you just need to read the file once.
您可以回到文件的开头,但更好的方法是重新排列两个循环,这样您只需要读取一次文件。
while (getline(fin, line)) {
for (int i = 0; i < 4; i++) {
if (line.find(eng2Str[i]) != string::npos) {
resArr[i]++;
}
}
}
I would suggest to use std::vector instead of pure C array. 我建议使用std :: vector而不是纯C数组。 In your code, there are more issues.
在您的代码中,还有更多问题。 You are passing the fourth element of both arrays to the engine2 function.
您将两个数组的第四个元素传递给engine2函数。 From your definition of
void engine2(string &eng2Str, int &resArr)
you expect reference to a string (not array / vector) and an address / reference of int - you need to pass an pointer to the first element of resArr. 根据你对
void engine2(string &eng2Str, int &resArr)
的定义,你期望引用一个字符串(不是数组/向量)和一个int的地址/引用 - 你需要将一个指针传递给resArr的第一个元素。
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <future>
using namespace std;
vector<string> eng2Str = { "money", "politics", "RT", "#" };
int resArr[4] = {};
void engine2(const vector<string>& eng2Str, int* resArr)
{
ifstream fin;
fin.open("sampleTweets.csv");
int fcount = 0;
string line;
for (int i = 0; i < 4; i++)
{
while (getline(fin, line))
{
if (line.find(eng2Str[i]) != string::npos)
{
++fcount;
}
}
resArr[i] = fcount;
}
fin.close();
return;
}
int main()
{
engine2(eng2Str, resArr);
system("Pause");
system("cls");
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.