2016-04-18 12 views
0

Ich verwende einen ifstream, um Eingaben aus einer Datei zu erhalten, wie in meinem Code unten zu sehen ist.Wie verwenden Sie in diesem Zusammenhang eine Referenz von ifstream? Fehler C2248

Ich realisiere, dass Sie nicht direkt ein ifstream greifen können, da es ein privates Mitglied ist. Worum geht es bei der Arbeit, um es durch Referenz zu erfassen und wie würde die Syntax aussehen?

Unten ist meine B.cpp und B.h, ich glaube nicht, das Anhängen der anderen Dateien ist notwendig, kann aber wenn es sein muss. Auch ich habe die Fehlermeldung darunter angefügt!

Vielen Dank für jede Hilfe im Voraus!

B.h:

#ifndef B_H 
#define B_H 
#include <string> 
#include <fstream> 
#include <sstream> 
#include <iomanip> 
#include <algorithm> 
#include <cctype> 
#include <iostream> 

class B { 
    public: 
     //Variable declarations 
     std::string line, delimiter, token, str; 
     std::ifstream inputfile; 
     size_t pos; 
     int lineRead; 

     //Function declarations 
     B(); 
     void setValues(int lineNum); 
     std::string printValues(); 
     bool to_bool(std::string str); 
    protected: 
     float f; 
     int i; 
     bool b; 
     std::string s; 
}; 

#endif 

B.cpp:

#include "B.h" 

B::B() { 

} 

void B::setValues(int lineNum) { 
    lineRead = 0; 
    pos = 0; 
    delimiter = " "; 

    inputfile.open("file.txt"); 

    while (!inputfile.eof()) { 
     //increment line counter 
     lineRead++; 

     //read line 
     getline(inputfile,line); 

     //if this is the line requested fill the template members with the 
     //correct data. <string, int, float, bool> 
     if(lineNum == lineRead){ 
      //getting the string 
      pos = line.find(delimiter); 
      token = line.substr(0, pos); 
      str = token; 
      line.erase(0, pos + delimiter.length()); 

      //getting the integer 
      pos = line.find(delimiter); 
      token = line.substr(0, pos); 
      i = stoi(token); 
      line.erase(0, pos + delimiter.length()); 

      //getting the float 
      pos = line.find(delimiter); 
      token = line.substr(0, pos); 
      f = stof(token); 
      line.erase(0, pos + delimiter.length()); 

      //getting the boolean 
      pos = line.find(delimiter); 
      token = line.substr(0, pos); 
      b = to_bool(token); 
      line.erase(0, pos + delimiter.length()); 
     } 
    } 

    //close the file 
    inputfile.close(); 
} 

std::string B::printValues() { 
    return s + " " + std::to_string(i) + " " + std::to_string(f) + " " + std::to_string(b) + "\n"; 
} 

//Changes a string to lower case and then reads the string 
//to find if the value is true or false. Returns a boolean. 
bool to_bool(std::string str) { 
    transform(str.begin(), str.end(), str.begin(), ::tolower); 
    std::istringstream is(str); 
    bool tempB; 
    is >> std::boolalpha >> tempB; 
    return tempB; 
} 

S.h:

#ifndef S_H 
#define S_H 
#include "B.h" // required to make B known here 
#include <string> // known through B, but better safe than sorry 
class S : public B { 

public: 
    S(std::string name); 
    std::string subPrint(); // *** 
protected: 
    std::string s2; 
}; 
#endif 

S.cpp:

#include "S.h" 

S::S(std::string name) : s2(name) { 

} 

std::string S::subPrint() { 
    return s2 + " " + printValues(); 
} 

Main.cpp:

#include "S.h" 
#include <vector> 

using namespace std; 

int main() { 
    int itPos = 0; 

    vector<S> v; 

    S s1("Jon"); 
    S s2("Mike"); 
    S s3("Kim"); 
    S s4("Steve"); 
    S s5("Kevin"); 

    v.push_back(s1); 
    v.push_back(s2); 
    v.push_back(s3); 
    v.push_back(s4); 
    v.push_back(s5); 

    cout << v[0].subPrint(); 

    system("pause"); 
    return 0; 
}; 

Error

+0

Was ist der eigentliche Fehler, den Sie bekommen? – NathanOliver

+0

Der Fehler ist in der Verknüpfung unter dem Code @NathanOliver. – jon

+0

Sorry vielleicht denke ich darüber falsch, @kfsone überprüfe meinen Fehler im Bild. – jon

Antwort

1

Sie hier Problem ist, dass std::ifstream nicht kopierbar ist. Diese Meanse B kann nicht kopiert werden und wiederum S kann nicht kopiert werden. Wenn Sie die S ‚s in den Vektor

v.push_back(s1); 
v.push_back(s2); 
v.push_back(s3); 
v.push_back(s4); 
v.push_back(s5); 

der Vektor hinzufügen versucht zu kopieren, was an sie übergeben wurde in den Vektor zu platzieren.

Eine einfache Lösung dafür ist, die ifstream von den Klassenmitgliedern zu löschen und nur als lokale Variable in setValues zu haben, da dies der einzige Ort ist, an dem es benutzt wird.

+0

Ich habe es lokal in setValues ​​() verschoben, aber ich erhalte diesen Fehler: Fehler Fehler LNK2019: nicht aufgelöstes externes Symbol "public: :: char_traits , Klasse std :: allocator >) "(? to_bool @ B @@ QAE_NV? $ basic_string @ DU? $ char_traits @ D @ std @@ V? $ Zuweiser @ D @ 2 @@ std @@@ Z) referenziert in der Funktion "public: void __thiscall B :: setValues ​​(int)" (? SetValues ​​@ B @@ QAEXH @ Z) 'und auch dieser Fehler dazu:' Fehler Fehler LNK1120: 1 nicht gelöst externals – jon

+0

@jon Haben Sie den Funktionsparameter für 'setValues' geändert? Sie sollten nur die Zeile 'std :: ifstream inputfile;' aus 'B' entfernen und dann' inputfile.open ("file.txt"); 'in' setValues' in 'ifstream inputfile (" file.txt ") ändern ");'. – NathanOliver

+0

Ja, ich habe es in 'std :: ifstream inputfile (" file.txt ") geändert;' – jon