如何在c ++中修复这些错误?

时间:2015-11-28 17:53:56

标签: c++

请不要讨厌,因为我还在学习,但我需要帮助将错误归类到这个程序。

#include <iostream>
#include <string>
#include "ConCommand.h"

using namespace std;

struct Command
{
    string usage;
    string command;
} command;

void SayCommand(string in)
{
    cout << in.substr(command.length() + 1, in.length()) << endl;
}

int main()
{
    string in;
    struct Command command1;

    command1.usage = "say <MESSAGE>";
    command1.command = "say";

    while (true)
    {
        cout << ">>> ";
        getline(cin, in);

        if (in == "")
        {
            cerr << "Please enter a command!" << endl;
        }

        ConCommand(in, command1.usage, command1.command, SayCommand());
    }

    return 0;
}

void ConCommand(string in, string usage, string command, void (*execFunc)(string))
{
    if (in.substr(0, command.length()) == command)
    {
        if (in.substr(command.length(), in.length()) != "")
        {
            //cout << in.substr(command.length() + 1, in.length()) << endl;
            execFunc(in);
        }
        if (in.substr(command.length(), in.length()) == "")
        {
            cout << "Usage: " + usage << endl;
        }
    }
}

(这个程序的主要目的是创建一个带参数的命令行,但是我不知道如何修复的两个问题仍然存在=第15行和第36行。)

谢谢。

1 个答案:

答案 0 :(得分:0)

我能看到的唯一错误是你需要将main移动到类的引导。 因此它看起来像这样。

#include <iostream>
#include <string>
#include "ConCommand.h"

using namespace std;

struct Command
{
   string usage;
   string command;
} command;

void SayCommand(string in)
{
   cout << in.substr(command.length() + 1, in.length()) << endl;
}



void ConCommand(string in, string usage, string command, void (*execFunc)(string))
{
    if (in.substr(0, command.length()) == command)
    {
       if (in.substr(command.length(), in.length()) != "")
       {
           //cout << in.substr(command.length() + 1, in.length()) << endl;
           execFunc(in);
       }
       if (in.substr(command.length(), in.length()) == "")
       {
           cout << "Usage: " + usage << endl;
       }
   }
}

   int main()
{
string in;
struct Command command1;

command1.usage = "say <MESSAGE>";
command1.command = "say";

while (true)
{
    cout << ">>> ";
    getline(cin, in);

    if (in == "")
    {
        cerr << "Please enter a command!" << endl;
    }

    ConCommand(in, command1.usage, command1.command, SayCommand());
}

return 0;
}

我还建议更清楚。这样你就可以简化并告诉我们它给你的错误是什么。