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

How do I fix these errors in c++?

本文关键字:错误 c++      更新时间:2023-10-16

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

#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行。)

谢谢。

我能看到的唯一错误是,您需要将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;
}

此外,我建议你要清楚得多。这样你就可以简化并告诉我们它给你的错误是什么。