C++ Class 不会执行函数
C++ Class won't execute function
我想写一个简单的备份程序。它还没有完成,但我遇到了一个问题:我的 class 负责设置正确的路径不会执行将复制文件的工作程序。我不知道为什么,是的 - 我已经在我知道的任何帮助网站上查找过。这是我的文件复制 h 代码:
#ifndef __FILECOPY_H_INCLUDED__
#define __FILECOPY_H_INCLUDED__
#include<iostream>
#include<fstream>
#include<ctime>
class filecopy
{
std::string dest_path;
std::string src_path;
public:
filecopy(std::string, std::string);
void filecopy_worker()
{
std::cout << "FILECOPY PROCESS STARTED" << std::endl;
std::ifstream source(src_path);
std::ofstream dest(dest_path);
dest << source.rdbuf();
source.close();
dest.close();
}
};
filecopy::filecopy(std::string a, std::string b)
{
dest_path = a;
src_path = b;
}
#endif
这是我的 main.cpp 代码:
#include<iostream>
#include<stdlib.h>
#include"filecopy.h"
int main(int argc, char *argv[])
{
if(argc != 3)
{
std::cout << "USAGE: " << argv[0] << " <filesource>" << std::endl;
return 1;
}
else
{
filecopy target1(argv[2], argv[1]);
std::cout << "TARGET ASSIGNED" << std::endl;
std::cout << "EXECUTE FILEWORKER" << std::endl;
}
return 0;
}
它没有执行该函数,因为您从未调用过它。只需添加该函数调用
filecopy target1(argv[2], argv[1]);
target1.filecopy_worker();
我想写一个简单的备份程序。它还没有完成,但我遇到了一个问题:我的 class 负责设置正确的路径不会执行将复制文件的工作程序。我不知道为什么,是的 - 我已经在我知道的任何帮助网站上查找过。这是我的文件复制 h 代码:
#ifndef __FILECOPY_H_INCLUDED__
#define __FILECOPY_H_INCLUDED__
#include<iostream>
#include<fstream>
#include<ctime>
class filecopy
{
std::string dest_path;
std::string src_path;
public:
filecopy(std::string, std::string);
void filecopy_worker()
{
std::cout << "FILECOPY PROCESS STARTED" << std::endl;
std::ifstream source(src_path);
std::ofstream dest(dest_path);
dest << source.rdbuf();
source.close();
dest.close();
}
};
filecopy::filecopy(std::string a, std::string b)
{
dest_path = a;
src_path = b;
}
#endif
这是我的 main.cpp 代码:
#include<iostream>
#include<stdlib.h>
#include"filecopy.h"
int main(int argc, char *argv[])
{
if(argc != 3)
{
std::cout << "USAGE: " << argv[0] << " <filesource>" << std::endl;
return 1;
}
else
{
filecopy target1(argv[2], argv[1]);
std::cout << "TARGET ASSIGNED" << std::endl;
std::cout << "EXECUTE FILEWORKER" << std::endl;
}
return 0;
}
它没有执行该函数,因为您从未调用过它。只需添加该函数调用
filecopy target1(argv[2], argv[1]);
target1.filecopy_worker();