Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;指向对象的数组指针错误_C++ - Fatal编程技术网

C++ C++;指向对象的数组指针错误

C++ C++;指向对象的数组指针错误,c++,C++,我有一个似乎很常见的问题,但是通过阅读对类似问题的回答,我根本找不到解决问题的方法,因为我已经做了他们建议的事情,例如将变量设置为数组。我有以下代码: #include "stdafx.h" #include <cstring> #include <fstream> #include <iostream> #include <string> #include <algorithm> #include <future> usi

我有一个似乎很常见的问题,但是通过阅读对类似问题的回答,我根本找不到解决问题的方法,因为我已经做了他们建议的事情,例如将变量设置为数组。我有以下代码:

#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;
}
#包括“stdafx.h”
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
字符串eng2Str[4]={“金钱”、“政治”、“RT”、“#”};
int resArr[4];
int main()
{
引擎2(eng2Str[4],resArr[4]);
系统(“暂停”);
系统(“cls”);
返回0;
}
无效引擎2(字符串和eng2Str、int和resArr)
{
流鳍;
财务公开(“sampleTweets.csv”);
int fcount=0;
弦线;
对于(int i=0;i<4;i++){
while(getline(fin,line)){
if(line.find(eng2Str[i])!=string::npos){
++F计数;
}
}
resArr[i]=fcount;
}
fin.close();
返回;
}
在您标记为副本之前,我已确保:

  • 我试图分配的数组和变量都是int
  • 这是一个数组
错误是:

表达式必须具有指向对象类型的指针

错误发生在“resArr[i]=fcount;”行,我不确定原因,因为resArr是一个int数组,我正在尝试从另一个int变量为其赋值。我对C++非常陌生,所以任何帮助都会很棒,因为我真的被卡住了。p> 谢谢

我建议使用而不是纯C数组。 在您的代码中,还有更多的问题。 您正在将两个数组的第四个元素传递给engine2函数。 从
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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
向量eng2Str={“金钱”、“政治”、“RT”、“#”};
int resArr[4]={};
无效引擎2(常量向量和eng2Str,int*resArr)
{
流鳍;
财务公开(“sampleTweets.csv”);
int fcount=0;
弦线;
对于(int i=0;i<4;i++)
{
while(getline(fin,line))
{
if(line.find(eng2Str[i])!=string::npos)
{
++F计数;
}
}
resArr[i]=fcount;
}
fin.close();
返回;
}
int main()
{
发动机2(发动机2、发动机2、发动机2);
系统(“暂停”);
系统(“cls”);
返回0;
}

问题在于,您已声明函数引用单个
字符串和
int
,而不是数组。应该是:

void engine2(string *eng2Str, int *resArr)
或:

然后在调用它时,可以将数组名称作为参数提供:

engine2(eng2Str, resArr);
另一个问题是函数中的
while
循环。这将在
for()
循环的第一次迭代期间读取整个文件。其他迭代将不会读取任何内容,因为它已经位于文件的末尾。您可以返回到文件的开头,但更好的方法是重新排列两个循环,以便只需读取一次文件

while (getline(fin, line)) {
    for (int i = 0; i < 4; i++) {
        if (line.find(eng2Str[i]) != string::npos) {
            resArr[i]++;
        }
    }
}
while(getline(fin,line)){
对于(int i=0;i<4;i++){
if(line.find(eng2Str[i])!=string::npos){
resArr[i]++;
}
}
}

你真的想在循环中放入一个循环吗?你必须粘贴准确的错误消息Post Compiled code。是的,我的意思是在循环中放入一个循环,因为我需要它在文件中搜索几个不同的关键字,计算它们一个接一个被使用的次数。我已经用我所有的东西更新了原始代码。将数组声明为维度4意味着有效索引为0到3。因此,当您稍后将
eng2Str[4]
编写为函数参数时,您正在访问越界。(与resArr[4]
相同)谢谢您的回复。循环中的变化让我有点困惑。我说的正确吗?它仍然会在整个文件中运行4次,搜索eng2str数组中的4个字符串中的每一个,并在每次找到时增加相应的resArr值?不正确。它会在文件中运行1次,并在每一行搜索4个字符串。哦,上帝,这是更有效的。。。先生,你太棒了,谢谢!我正在尝试在不同的函数中执行类似的操作,在该函数中,我搜索文件中的特定单词,但需要返回在其中找到的每一行。我想我需要一个多维数组,第一个是搜索的关键字,第二个是结果,但我不知道这是怎么回事。任何帮助都将是惊人的@KaiJones使用
std:map
while (getline(fin, line)) {
    for (int i = 0; i < 4; i++) {
        if (line.find(eng2Str[i]) != string::npos) {
            resArr[i]++;
        }
    }
}