2016-04-13 15 views
1

C++ 'ya oldukça yeni geliyorum ve şu anda şablonları öğrenmeye çalışıyor ve nasıl çalışıyorlar. Görevim bana bir şablona 4 argüman alan bir ana sınıf tabanı oluşturmamı istiyor. Ayrıca, temel sınıfın değerlerini yazdırabilecek bir alt sınıfa sahip olmalıdır.hata C2248, bu hata nedir ve nasıl düzeltebilirim?

Üst sınıf, txt dosyası okuyarak değerleri alır.

Şu anda bir alt sınıfı oluşturmaya çalışıyorum, böylece değerler satırını ana sınıfta satır olarak yazdırabiliyorum.

Kodum, ÇOK yeni ila C++ olduğu kadar dağınık. Arkaplan C#, Java ve Javascript'ten oluşur. Herhangi bir yardım harika olurdu! Şimdiden teşekkürler.

Hata Mesajı:

Error 1 error C2248: 'std::basic_ifstream<_Elem,_Traits>::basic_ifstream' : cannot access private member declared in class 'std::basic_ifstream<_Elem,_Traits>' 

Kodu:

#include <iostream> 
#include <string> 
#include <fstream> 
#include <sstream> 
#include <iomanip> 
#include <algorithm> 
#include <cctype> 

using namespace std; 

//template that accept 4 different data types 
template<class Type1, class Type2, class Type3, class Type4> 
class Base { 
    //protected declarations 
    protected: 
     Type1 str; 
     Type2 i; 
     Type3 d; 
     Type4 b; 

    //public declarations 
    public: 
     int lineRead; 
     int data; 
     string line, delimiter; 
     size_t pos; 
     ifstream inputfile; 
     string token; 

    //public functions 
    public: 
     void readFile(int lineNum) { 
      //temp information holders 
      lineRead = 0; 
      pos = 0; 
      delimiter = " "; 

      //open .txt file 
      inputfile.open("file.txt"); 

      //while end of file is not reached 
      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, double, 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 double 
        pos = line.find(delimiter); 
        token = line.substr(0, pos); 
        d = stod(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(); 
      system("pause"); 
     }; 

     //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(string str) { 
      transform(str.begin(), str.end(), str.begin(), ::tolower); 
      istringstream is(str); 
      bool tempB; 
      is >> boolalpha >> tempB; 
      return tempB; 
     } 
}; 

class Subclass : public Base<string, int, double, bool> { 
    private: 
     string name; 

    public: 
     Subclass::Subclass(string name) { 
      cout << "test"; 
     } 

     void printValues() { 
      cout << str; 
     } 


}; 

//main function 
int main() { 
    //call Base class and give the template data types 
    Subclass b = Subclass("test"); 

    //read the file 
    b.readFile(2); 

    return 0; 
} 

cevap

1
Subclass b = Subclass("test"); 

Bu, bir nesne oluşturur ve sonra kopyalamaya çalışır. Sınıfınız kopyalanamaz, dolayısıyla hata (ayrıca, türetilmiş sınıfları kopyalama, kendi yolunda problematic). Kopyalamadan nesne oluşturmak için aşağıdaki sözdizimini kullanın:

Subclass b("test"); 
+0

Basit hata, aptallığımı işaretlediğiniz için teşekkürler! :) –

0

Bir üye olarak bir std::ifstream var, ama onlar gösterdi olmayan copyable, dolayısıyla Hte hata mesajı konum. Pre-C++ 11, kopyalanamaz bir şey yapmanın deyimsel yolu, kopya kurucusunu özel erişim belirteci (gördüğünüz şey) ile beyan etmekti, böylece hiç kimse yanlışlıkla kullanamazdı. C++ 11'de, delete anahtar sözcüğü tercih edilir ve "silinen işlev kullanımı" satırlarında bir hata alırsınız.

You should ya:

  1. sahip değil olmayan bir copyable üyesi bir kopya kurucu tanımlayın ve atama operatöre
  2. kopya

Birlikte gider (2) Yerinde olsam; Kendinize bunun neden bir üyeye ihtiyaç duyduğunu sorun ve daha sonra bunu hesaba katmanın bir yolunu bulun.