implementing function in a class causes error: member reference base type 'ifstream (string)' is not a structure or union

I'm trying to implement a function that reads a column of data from a text file and stores it in a vector, which works. However when I try to implement it inside of a class I'm clearly missing some step. This causes the terminal to output the following message:

Outout for

error: member reference base type
      'ifstream (string)' is not a structure or union
                ...

error: member reference base type
      'ifstream (string)' is not a structure or union
                while(!file.eof()){
                ..
error: invalid operands to binary
      expression ('ifstream (*)(string)' and 'double')
                        file >> line;

In my class I try to implement the following function to be used with it's data members:

#include <iostream>
#include <vector>
#include <stdio.h>
#include <fstream> 

using namespace std;

class spectData{
public:
    vector<double> x, y, z;
    vector< int> A;
    vector<double> readVector(string){
        ifstream file(string);
        double line;
        vector<double> a;

        if(file.fail()){
            cout << "-----------------\n";
            cout << "Input file error!\n";
        }
        while(!file.eof()){
            file >> line;
            a.push_back(line);
        }
        return a;
    };
};

Any hint as to why this wouldn't work inside a function, but would inside main function?

Answers


The issues in this code are numerous, including:

  • Failing to include <string>. Don't rely on another header to do that for you.
  • Invalid parameter naming (as in, you have none; remember parameters are Type name).
  • Building on the mistake from above, ifstream file(string); therefore declares a function called file that takes a string parameter and returns an ifstream (which is impossible, as that class does not support copy construction, but does support move construction, not that it matters here).
  • Using .eof() as a loop condition, which is nearly always wrong (and this is no exception). Read this for why.
  • Minor: Reinventing the iterative read operation. std::istream_iterator provides this functionality for you, and should be exploited.
  • Minor: blanketing this with using namespace std;

For example:

#include <iostream>
#include <fstream> 
#include <string>
#include <vector>
#include <iterator>

class spectate
{
public:
    std::vector<double> x, y, z;
    std::vector< int> A;

    std::vector<double> readVector(const std::string& fname)
    {
        std::vector<double> res;

        std::ifstream file(fname);
        if(!file)
        {
            std::cout << "-----------------\n";
            std::cout << "Input file error!\n";
        }
        else
        {   // dump the file of doubles into your vector
            std::copy(std::istream_iterator<double>(file),
                      std::istream_iterator<double>(),
                      std::back_inserter(res));
        }
        return res;
    }
};

using namespace std;  
...
vector<double> readVector(string){
//                        ~~~~~~^
//                        missing parameter name
    ifstream file(string);
//                ~~~~~^
//                whoops, type name aka std::string instead of parameter name

What your ifstream file(string); currently does, it declares a function file that takes by value a parameter of the std::string type and returns the std::ifstream instance. Hence the error you get. What you probably meant to do is to supply a path parameter to your file's constructor:

vector<double> readVector(const string& path){
//                        ~~~~~~~~~~~~~~~~~^
//                        parameter name
    ifstream file(path.c_str());
//                ~~~^ ~~~~~~^
//                 

Need Your Help

How to set ASP.NET API with test in separate project to use one IoC/DI container

c# asp.net-mvc unit-testing asp.net-web-api mocking

I'm quite new to unit testing and I struggle one thing. I want to create WebAPI in ASP.NET MVC with test project containing test methods for the controllers. I've read quite a lot about IoC/DI and ...

Combining two forms in one Django view

python django

I am working on adding more functionality to the polls app that is made in the official Django tutorial. One of the things I am working on is making Polls/Choices creatable by logged in users (inst...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.