简体   繁体   中英

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 array and variable I am trying to assign are both int
  • Its an array

The error is:

expression must have pointer-to-object type

The error is occurring at the "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. I am quite new to C++ so any help would be great as I am really stuck!

Thanks!

The problem is that you've declared your function to take a reference to a single string and int , not arrays. 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. This will read the entire file during the first iteration of the for() loop. 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. In your code, there are more issues. You are passing the fourth element of both arrays to the engine2 function. 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.

#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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM