简体   繁体   中英

How do I fix these errors in c++?

Please don't hate as I am still learning but I need help sorting the errors to this program.

#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;
        }
    }
}

(The main aim of this program is to create a command line with arguments, but two problems that I have no clue how to fix keep being there = line 15 & line 36.)

Thank you.

The only error I can see is that you need to move main to the boot of the class. Thus it will look like this.

#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;
}

Also I would recommend being much much more clear. Such that you simplify and tell us what the error that it is giving you is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM