2017-05-19 2 views
-1

Im Moment arbeite ich an einer kleinen Konsole App, ich lese von einigen Dateien, und ich habe einen einfachen animierten Ladebalken Thread gemacht, um den Fortschritt der Datei lesen zu zeigen (manchmal die Datei sind wirklich riesig)Visual Studio 2015 C2011 'Klassen' Typ Neudefinition

Ich habe auf der Microsoft-Dokumentation und im Forum gelesen, und dieser Fehler scheint darauf hinzuweisen, dass ich die Klasse mehrere Male definiert habe.

aber ich habe alle Header-Blöcke enthalten, um zu verhindern, dass dies geschieht, jeder von euch sieht meinen Fehler, wahrscheinlich offensichtlich, ich habe nicht C++ in Jahren getan.

Hier ist der Code

fileReader.h

#pragma once 
#ifndef FILEREADER_H // must be unique name in the project 
#define FILEREADER_H 

#include <vector> 

using namespace std; 

class fileReader { 

private: 

public: 
    vector<string> readTextFile(string path); 
    unsigned int getSize(); 
    unsigned int getLoadedBytes(); 
}; 
#endif  // FILEREADER_H 
#pragma once 

fileReader.cpp

#include "stdafx.h" 
#include <stdio.h> 
#include <stdlib.h> 
#include <iostream> 
#include <fstream> 
#include <string> 
#include <vector> 
#include <thread> 

#include "numbers.h" 
#include "loadingBar.h" 


using namespace std; 

class fileReader { 
private: 

public : 
    vector<string> list; 
    int i; 
    unsigned int size; 
    unsigned int loadedBytes; 
    string line; 
    std::thread* barThread; 

    vector<string> fileReader::readTextFile(string path) { 

     ifstream file(path.c_str(), ios::in | ios::binary | ios::ate); 

     i = 0; 
     size = 0; 
     loadedBytes = 0; 

     ifstream file(path.c_str(), ios::in | ios::binary | ios::ate); 

     if (file.is_open()) { 
      size = file.tellg(); 
      cout << "\t" << path << " (" << formatNumbers(size) << " bytes)" << endl; 
      file.clear(); 
      file.seekg(0, ios::beg); 

      barThread = new std::thread(&loadingBar::run, *this); 

      while (file >> line) { 
       list.push_back(line); 
       loadedBytes += strlen(line.c_str()); 
       i++; 
      } 

     } 
     else { 
      cout << "Error reading : \"" << path << "\"" << endl; 
      exit(EXIT_FAILURE); 
     } 
     file.close(); 
     return list; 
    } 

    unsigned int fileReader::getSize() { return size; } 
    unsigned int fileReader::getLoadedBytes() { return loadedBytes; } 

}; 

loadingBar.h

#pragma once 
#ifndef LOADINGBAR_H 
#define LOADINGBAR_H 

#include<stdlib.h> 
#include<string> 
#include<iomanip> 
#include "numbers.h" 
#include "fileReader.h" 

using namespace std; 

class loadingBar { 
public: 
    void notififyFinish(); 
    void notifyError(); 
    void notifiError(string); 
    void drawloadingBar(); 
    void drawPathBar(); 
    void drawBytesBar(); 
    void setLoadedUnits(int); 
    void setTotalUnits(int); 
    void run(fileReader*); 

}; 

#endif // NUMBERS_H 

loadingBar.cpp

#include "stdafx.h" 
#include <stdio.h> 
#include <iostream> 
#include <stdlib.h> 
#include <string> 
#include <iomanip> 

#include "numbers.h" 
#include "fileReader.h" 

using namespace std; 

class loadingBar { 

private: 

public: 

    double totalUnits = 1; 
    double loadedUnits = 0; 
    char loadedChar = '/'; 
    char emptyChar = ' '; 
    int barLength = 50; 
    double percent; 
    int nbChar; 

    void notiftyFinish() { 
     cout << " Ok" << endl; 
     //exit(EXIT_SUCCESS); 
    } 

    void notifiyError() { 
     cout << " Error !" << endl; 
     //exit(EXIT_FAILURE); 
    } 

    void notifiyError(string errMess) { 
     cout << " Error !" << endl << "\t" << errMess; 
     //exit(EXIT_FAILURE); 
    } 

    void drawLoadingBar() { 
     cout << fixed; 

     cout << "\rLoading ["; 

     for (int i = 0; i < nbChar; i++) 
      cout << loadedChar; 

     for (int i = nbChar; i < barLength - 1; i++) 
      cout << emptyChar; 

     cout << "] " << setprecision(0) << percent << "%"; 
    } 

    void drawPathBar(string path) { 
     cout << fixed; 

     cout << "\rLoading [ "; 

     cout << path; 

     cout << " ] " << setprecision(0) << percent << "%"; 
    } 

    void drawBytesBar() { 
     cout << fixed; 

     cout << "\rLoading [ "; 

     cout << formatNumbers(loadedUnits) << "/" << formatNumbers(totalUnits); 

     cout << " ] " << setprecision(0) << percent << "%"; 
    } 

    void setLoadedUnits(int newValue) { 
     if (newValue > 0) 
      loadedUnits = newValue; 
    } 

    void setTotalUnits(int value) { 
     if (value > 0) 
      totalUnits = value; 
    } 

    void run(fileReader *f) { 
     setTotalUnits(f->getSize()); 
     setLoadedUnits(f->getLoadedBytes()); 

     while (loadedUnits <= totalUnits) { 
      setLoadedUnits(f->getLoadedBytes()); 
      percent = ((double)(loadedUnits/totalUnits) * 100); 
      nbChar = (int)(percent/(int)(100/barLength)); 

      drawLoadingBar(); 

      //setLoadedUnits((int)loadedUnits + 10); 

      if (loadedUnits >= totalUnits) notiftyFinish(); 
     } 
    } 
}; 
+3

Sie deklarieren die Klasse in Ihrer Implementierungsdatei? Was ist eigentlich unklar an dieser Fehlermeldung? Stoppen Sie auch, 'using namespace std;' zu verwenden, insbesondere in Header-Dateien. Ich bin mir ziemlich sicher, dass das eine doppelte Frage ist, nur um zu faul zu sein, jetzt zu suchen (das ist deine Verantwortung überhaupt). –

+2

Du deklarierst die Klassen in Bot h Header und CPP. Deshalb erhalten Sie den Fehler. Überprüfen Sie, wie die Implementierung in den CPP-Dateien durchgeführt wird, und Sie werden es loswerden. Keine 'Klasse ...' dort benötigt und alle Membervariablen zum Header. –

Antwort

0

Ihre .cpp-Dateien definieren Ihre Klassen neu. Sie haben sie bereits in den entsprechenden .h-Dateien definiert. Alles, was Sie in Ihre CPP-Dateien aufnehmen müssen, sind die Implementierungen. Sie sollten mehr wie folgt aussehen:

fileReader.cpp

#include "stdafx.h" 
#include <stdio.h> 
#include <stdlib.h> 
#include <iostream> 
#include <fstream> 
#include <string> 
#include <vector> 
#include <thread> 

#include "numbers.h" 
#include "loadingBar.h" 

vector<string> fileReader::readTextFile(string path) { 

    ifstream file(path.c_str(), ios::in | ios::binary | ios::ate); 

    i = 0; 
    size = 0; 
    loadedBytes = 0; 

    ifstream file(path.c_str(), ios::in | ios::binary | ios::ate); 

    if (file.is_open()) { 
     size = file.tellg(); 
     cout << "\t" << path << " (" << formatNumbers(size) << " bytes)" << endl; 
     file.clear(); 
     file.seekg(0, ios::beg); 

     barThread = new std::thread(&loadingBar::run, *this); 

     while (file >> line) { 
      list.push_back(line); 
      loadedBytes += strlen(line.c_str()); 
      i++; 
     } 

    } 
    else { 
     cout << "Error reading : \"" << path << "\"" << endl; 
     exit(EXIT_FAILURE); 
    } 
    file.close(); 
    return list; 
} 

unsigned int fileReader::getSize() { return size; } 
unsigned int fileReader::getLoadedBytes() { return loadedBytes; } 

Ebenso für loadingBar.cpp.