c++ - Visual Studio 2015 C2011 'Class' 类型重定义

标签 c++ class

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

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

但我确实包含了所有标题 block 以防止这种情况发生,你们中的任何人都看到了我的错误,很明显,我已经多年没有使用 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; }

同样适用于 loadingBar.cpp

关于c++ - Visual Studio 2015 C2011 'Class' 类型重定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44079323/

相关文章:

c++ - 我应该如何编写类? C++

c++ - 初始化成员变量后加括号?

c++ - 存储 2D 点以便快速检索矩形内的点

c++ - Doxygen 混淆了函数文档和内部类文档

c++ - 为什么我的 vector 迭代器不兼容?

java - 数组列表中丢失数据

javascript - 关于继承我不明白的几件事

java - Android onLocationChanged 和 MainActivity 类

c++ - operator== 如何在 C++ 类之外工作

c++ - 在哪个 ISO C++ 版本中引入了迭代器?