【问题标题】:C++ Class won't execute function [closed]C ++类不会执行函数[关闭]
【发布时间】:2016-06-03 20:14:03
【问题描述】:

我想写一个简单的备份程序。它还没有完成,但我遇到了一个问题:我负责设置正确路径的班级不会执行将复制文件的工作人员。我不知道为什么,是的 - 我已经在我知道的任何帮助网站上查找过。这是我的文件复制 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;
}

【问题讨论】:

  • 你为什么会期望它?我没有看到您调用任何会复制任何内容的代码。
  • 我尝试将其称为成员函数,但这不起作用。我在复制和粘贴之前删除了它。我不明白你为什么生气——这个网站不是一个专属俱乐部。我可以问一些基本的问题。说真的 - 在某些时候你是一个初学者并且错过了这样的事情。要有礼貌。

标签: c++ function class member


【解决方案1】:

它没有执行该函数,因为您从未调用过它。只需添加该函数调用

filecopy target1(argv[2], argv[1]);
target1.filecopy_worker();

【讨论】:

  • 题主有具体问题,我给出了具体的小答案。这有什么问题?
  • 非常感谢!你拯救了我的一天。是的。这是一个愚蠢的问题,但我是初学者,你给了一个很好的答案。
  • @CoryKramer:鼓励更多琐碎的废话,这是我的猜测。我们需要对 SO 进行事先研究。请注意,虽然我同意你不应该回答,但我认为我们不应该使用投票来惩罚它。
  • @LightnessRacesinOrbit,是的,这就是原因。这个问题对未来的读者有用的机会正好是 0,那么为什么我们鼓励这样做呢?至于投反对票,如果答案没有被投反对票,我不会投反对票 - 因此鼓励更多这样的答案。
  • @SergeyA:根据现有分数投票也是错误的;p
猜你喜欢
  • 1970-01-01
  • 2014-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-21
  • 1970-01-01
相关资源
最近更新 更多