gpt4 book ai didi

c++ - 错误:该类(class)没有名为 'ThisW'的成员;你是说 'This'吗?但是代码没有说 'ThisW'它已经说了 'This'

转载 作者:行者123 更新时间:2023-12-02 10:07:44 24 4
gpt4 key购买 nike

通过将CreateFile重命名为MakeFile来解决

我对c++缺乏经验,并且正在将GUI和现有项目结合在一起,因此可能会有很多问题,但是我现在无法解决的主要问题是此“无成员”错误。

错误是:

'class FileHandler' has no member named 'CreateFileW'; did you mean 'CreateFile'?

main.cpp:51:32: error: no member named 'CreateFileW' in 'FileHandler'

fileapi.h:31:20: note: expanded from macro 'CreateFile'

_mingw_unicode.h:12:32: note: expanded from macro '__MINGW_NAME_AW'

2:1: note: expanded from here

但是,代码本身并没有说CreateFileW,所以W来自哪里?

Main.cpp -错误最多
#include <QApplication>
#include <iostream>
#include <vector>
#include <string>
#include <stdlib.h> // exit
#include <cstdio> // File


#include "file_handler.h"
#include "data_store.h"
#include "cpl_data_store.h"
#include "opl_data_store.h"
#include "data_processor.h"
#include "view.h"
#include "file.h"
#include "screen.h"
#include "structs.h"
#include "dialog.h"

int main(int argc, char *argv[]) {

QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();

// create filehandler object that will handle file operations
FileHandler fh;

// get the filename given by the user using command line arguments
std::string filename = "Test"; //fh.GetFilename(argc, argv);

// open file for reading
FILE * ballot_file = fopen(filename.c_str(), "r");

// file validation, check file exist, if not output error message and exit program
if (ballot_file == NULL) {
std::cout << "Error. File cannot be opened." << std::endl;
exit(EXIT_FAILURE);
}

// read from file, returns a vector with all the data, so size of vector should = number of lines
// possible bug here if number of lines in file is > vector max size
std::vector<std::string> file_info = fh.ReadFromFile(ballot_file);

// done reading, close ballot file
fclose(ballot_file);

// create audit file
File audit_file("audit.txt", "textfile");
FILE * audit_file_ptr = fh.CreateFile(audit_file.GetName());

// create media file
File media_file("media.txt", "textfile");
FILE * media_file_ptr = fh.CreateFile(media_file.GetName());

file_handler.cc
/**
* @file file_handler.cc
*
* @copyright 2019 Team32, All rights reserved.
*/
#include <string>
#include <iostream>
#include <cstdio> // File
#include <vector>

#include "file_handler.h"

std::string FileHandler::GetFilename(int argc, char const *argv[]) {
std::string filename;

if (argc == 2) {
filename = argv[1];
} else if (argc == 1) { // no input file given
// output error message
std::cout << "Error. Please provide a filename." << std::endl;
// get filename
std::cout << "Enter a filename: ";
std::cin >> filename;
} else { // more than 2 arguments given
// output error message
std::cout << "Error. " << argc << " arguments given. 2 arguments expected." << std::endl;
// get filename
std::cout << "Enter a filename: ";
std::cin >> filename;
}

return filename;
}

std::vector<std::string> FileHandler::ReadFromFile(FILE * file_pointer) {
std::vector<std::string> file_info; // stores lines read in

char str[500]; // hold the line read in

while (fscanf(file_pointer, "%s", str) != EOF) {
file_info.push_back(str);
}

return file_info;
}

FILE * FileHandler::CreateFile(std::string filename) {
return fopen(filename.c_str(), "w+");
}

void FileHandler::WriteToFile(FILE *file_pointer, std::string str) {
std::string output = str + "\n";
fprintf (file_pointer, output.c_str());
}

file_handler.h
/**
* @file file_handler.h
*
* @copyright 2019 Team32, All rights reserved.
*/
#ifndef SRC_FILE_HANDLER_H_
#define SRC_FILE_HANDLER_H_

/*******************************************************************************
* Includes
******************************************************************************/
#include <string>
#include <cstdio> // File
#include <vector>

/*******************************************************************************
* Class Definitions
******************************************************************************/
/**
* @brief Class that store all the election data read in from a file.
*/
class FileHandler {
public:
/**
* @brief Gets the name of the user given file from the terminal.
*
* @param[in] int number of arguments given from the command line
* @param[in] char * an array that contains arguments from the command line
*
* @return the input filename
*/
std::string GetFilename(int argc, char const *argv[]);
/**
* @brief Read from a file
*
* @param[in] File * pointer to a file that we want to read from
*
* @return a vector with each element being a line read in from a file
*/
std::vector<std::string> ReadFromFile(FILE * file_pointer);
/**
* @brief Creates a new file
*
* @param[in] std::string name of file that should be created
*
* @return File * to the file created
*/
FILE * CreateFile(std::string filename);
/**
* @brief Write a string to a file
*
* @param[in] File * pointer to a file that should be written in
* @param[in] std::string string to be written into the file
*/
void WriteToFile(FILE *file_pointer, std::string str);
};
#endif // SRC_FILE_HANDLER_H_

最佳答案

您可能包括了一些Windows库,其中 CreateFile 是指向不同功能的#define,即CreateFileW。我认为最简单的解决方案是将函数重命名为Create或其他名称,或者MakeFile或其他名称。我想您会发现通过重命名它可能会解决您的问题。

Windows有一个称为#definemin,这会严重破坏std::min的使用,这存在一个非常相似的问题。

关于c++ - 错误:该类(class)没有名为 'ThisW'的成员;你是说 'This'吗?但是代码没有说 'ThisW'它已经说了 'This',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59315969/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com