C++ 如何在C++;

C++ 如何在C++;,c++,arrays,C++,Arrays,我正在尝试为一个学校项目编写一个命令行界面。我有一个驱动程序.cpp来运行命令行,它将命令解析为命令解释器的字符串数组。当我尝试调试时,调用解释器->解释(rowData,length)时,似乎只传递了第一个字符串如果我在命令行中键入“import p filename”,它看起来像args只包含字符串“import”。我希望它被传递{“导入”、“p”、“文件名”}。我是否错误地传递或访问字符串数组 驱动程序.cpp #include "CommandInterpreter.h" #includ

我正在尝试为一个学校项目编写一个命令行界面。我有一个
驱动程序.cpp
来运行命令行,它将命令解析为
命令解释器的字符串数组。当我尝试调试时,调用
解释器->解释(rowData,length)时,似乎只传递了第一个字符串
如果我在命令行中键入“import p filename”,它看起来像
args
只包含字符串“import”。我希望它被传递{“导入”、“p”、“文件名”}。我是否错误地传递或访问字符串数组

驱动程序.cpp

#include "CommandInterpreter.h"
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main() 
{
   CommandInterpreter * interpreter = new CommandInterpreter();

   while ( !interpreter->terminate() )
   {
      string cmd;
      getline(cin, cmd);

      //Parse command args to lowercase
      int i = 0;
      while(cmd[i])
      {
         char c = cmd[i];
         putchar(tolower(c));
         i++;
      }
      //Parse command line into command and parameters.
      //Space delimited.
      stringstream ss(cmd);
      string item;
      string * rowData = new string[100];
      int length = 0;
      while (getline(ss, item, ' ')) {
         rowData[length] = item;
         length++;
      }

      //Interpret command
      interpreter->interpret(rowData, length);
      //Print feedback
      cout << interpreter->feedback() << '\n';
   }

   delete interpreter;
   return 0;
}
#include "CommandInterpreter.h"

CommandInterpreter::CommandInterpreter(void)
{
   exitFlag = false;
   handback = "";
   PC = new ProjectController();
   LC = new LocationController();
   VC = new VolunteerController();
   SG = new ScheduleGenerator();
   VS = new VolunteerScheduler();
}


CommandInterpreter::~CommandInterpreter(void)
{
   delete PC;
   delete LC;
   delete VC;
   delete SG;
   delete VS;
}


bool CommandInterpreter::terminate(void)
{
   return this->exitFlag;
}


bool CommandInterpreter::interpret(string * args, int length)
{
   //args should be an array of strings like {"import","p","filename"}
   //Debug only shows "import"

   string cmd = args[0];
   handback = "";

   if(cmd == "exit")
   {
      exitFlag = true;
   }
   else if(cmd == "save")
   {
      this->save(args[1][0]);
   }
   else if(cmd == "import")
   {
      this->import(args[1][0], args[2]);
   }
   else if(cmd == "print")
   {
      if(length == 2)
         this->print(args[1][0], '0');
      else
         this->print(args[1][0], args[2][0]);
   }
   else if(cmd == "schedule")
   {
      this->schedule(args[1][0]);
   }
   else if(cmd == "help")
   {
      this->help();
   }
   else
   {
      this->handback = "Invalid Command. Type help to see commands.";
      return false;
   }

   return true;
}


string CommandInterpreter::feedback(void)
{
   return this->handback;
}


bool CommandInterpreter::save(char arg0)
{
   bool success = true;

   switch (arg0)
   {
   case 'p':
      // PC->save();
      handback = "Saved project schedule.";
      break;
   case 'v':
      // VC->save();
      handback = "Saved volunteer schedule.";
      break;
   default:
      // Uh-oh
      handback = "Invalid argument for save: " + arg0;
      success = false;
      break;
   }
   return success;
}

bool CommandInterpreter::import(char arg0, string filename)
{
   bool success = true;
   switch (arg0)
   {
   case 'p':
      PC->importCSV(filename);
      handback = "Imported projects file: " + filename;
      break;
   case 'l':
      LC->importCSV(filename);
      handback = "Imported locations file: " + filename;
      break;
   case 'v':
      VC->importCSV(filename);
      handback = "Imported volunteers file: " + filename;
      break;
   default:
      success = false;
      handback = "Invalid argument for import command: " + arg0;
      break;
   }
   return success;
}


bool CommandInterpreter::print(char arg0, char arg1)
{
   bool success = true;
   switch (arg0)
   {
   case 'p':
      // PC->print()
      // or project schedule print
      // depending on arg1
      if(arg1 == '0')
         handback = PC->getProjectStrings();
      else
         ;   //Print project schedule here.
      break;
   case 'l':
      // LC->print()
      break;
   case 'v':
      // VC->print()
      // or volunteer schedule print
      // depending on arg1
      break;
   default:
      success = false;
      handback = "Invalid argument for print command: " + arg0;
      break;
   }
   return success;
}


bool CommandInterpreter::schedule(char arg0)
{
   bool success = true;
   switch (arg0)
   {
   case 'p':
      // SG->generate()
      handback = "Project schedule generated.";
      break;
   case 'v':
      // VS->generate()
      handback = "Volunteer schedule generated.";
      break;
   default:
      success = false;
      handback = "Invalid argument for schedule command: " + arg0;
      break;
   }
   return success;
}


bool CommandInterpreter::help(void)
{
   return false;
}

哇,这么多问题,这么少时间

不必要的动态内存分配
commandexplorer*explorer=newcommandexplorer()
您使用的
std::string
没有动态分配,那么为什么在这里使用这个呢? 此外,如果必须动态分配,则应使用具有指针和内存管理处理的
唯一\u ptr
boost::scoped_ptr

如果对象对于局部变量来说太大,则将其声明为
static

将“\0”用于std::字符串终止
“\0”是C样式的字符串终止符。使用
getline
时,它用文本填充
std::string
对象。
std::string
不使用nul字符终止字符串。将
while
循环更改为使用迭代器,或将其设置为
for
循环的长度:

for (unsigned int i = 0; i < cmd.length(); ++i)
{
  cmd[i] = std::tolower(cmd[i]);
}
传递数组
请记住,数组名称可能会衰减到第一个插槽的地址。此外,还需要将容量与阵列中的元素数一起传递:

void CommandInterpreter(string * array, unsigned int items_in_array, unsigned int array_capacity);

在为C语言编译时,头文件会做一些奇怪的事情

一些约定使用“.h”或“.HPP”表示头文件是C++的。p> 提高变量名的可读性
这两个字母变量不可读。选择更长的变量名。如果你选择了两个字母的缩写,因为它太长,无法打字,那就去上键盘课

使用初始化列表
您可以在初始化列表中使用
new
运算符,这提醒我

检查内存分配是否有错误
需要检查每个动态内存分配是否成功。这可以通过检查指针是否为NULL或捕获异常来执行。还要验证您的实现设置是否基于
new
引发异常。某些编译器允许您关闭异常引发

并非每个函数都需要位于对象中
一些面向对象的语言强制执行函数必须在类中的规则。C++语言不同,可以在类之外的函数中称为“自由站立函数”。试试看


现在就足够了…

为什么要添加所有指针和
新的
?有人告诉过你这样做吗?看起来像是毫无意义的对象化……看起来好像有人在C++中使用java或C语言技巧。<代码> ARGs<代码>是一个变量,类型<代码> STD::Stry*<代码>。我怀疑它包含任何字符串。你知道C++中没有垃圾回收器吗?
std::vector<std::string> commands;  // Look Ma, no *new* operator.
CommandInterpreter(commands);
void CommandInterpreter(string * array, unsigned int items_in_array, unsigned int array_capacity);
void CommandInterpreter(std::vector<std::string>& array); // Much simpler using vector.
typedef struct class
{
  unsigned int cout;
  double       private;
} class_t;