Visual Studio 2015 C2011 'Class' 类型重定义

Visual Studio 2015 C2011 'Class' type redefinition

本文关键字:类型 定义 Class Studio 2015 Visual C2011      更新时间:2023-10-16

im 目前正在开发一个小控制台应用程序,我从一些文件中读取,并且我制作了一个简单的动画加载栏线程来显示文件读取的进度(有时文件真的很大)

我已经阅读了Microsoft文档和论坛,该错误似乎表明我多次定义了该类。

但是我确实包含了所有的标题块以防止这种情况发生,你们中的任何一个人都看到了我的错误,可能很明显,我已经很多年没有做过C ++了。

这是代码

文件读取器.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

文件读取器.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; }
};

加载吧.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

加载栏.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();
}
}
};

您的.cpp文件重新定义了您的类。您已经在相应的 .h 文件中定义了它们。您需要包含在.cpp文件中的只是实现。它们应该看起来更像这样:

文件读取器.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; }

同样用于加载栏.cpp