三行代码  ›  专栏  ›  技术社区  ›  Bee

由于更改函数的输出数据类型而导致编译错误

c++
  •  0
  • Bee  · 技术社区  · 1 周前

    请原谅我这是一个明显的解决办法,因为我在C++方面是新手。

    在我的任务中,我们得到了一些模拟命令行加密交易平台的起始代码。最初,系统会提示您键入命令1到6,以执行诸如打印exchange统计数据、报价、打印钱包等操作。在我的作业中,我被要求创建一个advisor机器人,它接收字符串命令(如“help”)并处理这些命令以返回相应的输出。

    在做其他事情之前,我想知道为什么这个函数 getUserOption 输入 在init函数的while循环中带红色下划线,但在实际函数中没有错误:

    enter image description here

    enter image description here

    这是头文件的内容

    #pragma once
    
    #include <vector>
    #include "OrderBookEntry.h"
    #include "OrderBook.h"
    #include "Wallet.h"
    #include <string>
    using namespace std;
    
    class MerkelMain
    {
        public:
            MerkelMain();
            /** Call this to start the sim */
            void init();
        private: 
            void advisorBot();
            void printHelp();
            void printMarketStats();
            void enterAsk();
            void enterBid();
            void printWallet();
            void gotoNextTimeframe();
            std::string getUserOption();
            void processUserOption(string userOption);
            void printCurrentTime();
    
            std::string currentTime;
    
    //        OrderBook orderBook{"20200317.csv"};
        OrderBook orderBook{"20200601.csv"};
            Wallet wallet;
    
    };
    

    这是cpp文件

    #include "MerkelMain.h"
    #include <iostream>
    #include <vector>
    #include "OrderBookEntry.h"
    #include "CSVReader.h"
    #include <string>
    using namespace std;
    
    MerkelMain::MerkelMain()
    {
    
    }
    
    void MerkelMain::init()
    {
        int input;
        currentTime = orderBook.getEarliestTime();
    
        wallet.insertCurrency("BTC", 10);
    
        while(true)
        {
            advisorBot();
            input = getUserOption();
            processUserOption(input);
        }
    }
    
    
    void MerkelMain::advisorBot()
    {
        
        std::cout << "Please enter a command, or help for a list of commands" << std::endl;
        std::cout << "============== " << std::endl;
        std::cout << "Current time is: " << currentTime << std::endl;
    }
    
    /*void MerkelMain::printMenu()
    {
        // 1 print help
        std::cout << "1: Print help " << std::endl;
        // 2 print exchange stats
        std::cout << "2: Print exchange stats" << std::endl;
        // 3 make an offer
        std::cout << "3: Make an offer " << std::endl;
        // 4 make a bid 
        std::cout << "4: Make a bid " << std::endl;
        // 5 print wallet
        std::cout << "5: Print wallet " << std::endl;
        // 6 continue   
        std::cout << "6: Continue " << std::endl;
    
        std::cout << "============== " << std::endl;
    
        std::cout << "Current time is: " << currentTime << std::endl;
    }*/
    
    void MerkelMain::printHelp()
    {
        std::cout << " The available commands are help, help cmd, prod, min, max, avg, predict, time, step  " << std::endl;
    }
    
    void MerkelMain::printMarketStats()
    {
        for (std::string const& p : orderBook.getKnownProducts())
        {
            std::cout << "Product: " << p << std::endl;
            std::vector<OrderBookEntry> entries = orderBook.getOrders(OrderBookType::ask, 
                                                                    p, currentTime);
            std::cout << "Asks seen: " << entries.size() << std::endl;
            std::cout << "Max ask: " << OrderBook::getHighPrice(entries) << std::endl;
            std::cout << "Min ask: " << OrderBook::getLowPrice(entries) << std::endl;
    
    
    
        }
    
    }
    
    void MerkelMain::enterAsk()
    {
        std::cout << "Make an ask - enter the amount: product,price, amount, eg  ETH/BTC,200,0.5" << std::endl;
        std::string input;
        std::getline(std::cin, input);
    
        std::vector<std::string> tokens = CSVReader::tokenise(input, ',');
        if (tokens.size() != 3)
        {
            std::cout << "MerkelMain::enterAsk Bad input! " << input << std::endl;
        }
        else {
            try {
                OrderBookEntry obe = CSVReader::stringsToOBE(
                    tokens[1],
                    tokens[2], 
                    currentTime, 
                    tokens[0], 
                    OrderBookType::ask 
                );
                obe.username = "simuser";
                if (wallet.canFulfillOrder(obe))
                {
                    std::cout << "Wallet looks good. " << std::endl;
                    orderBook.insertOrder(obe);
                }
                else {
                    std::cout << "Wallet has insufficient funds . " << std::endl;
                }
            }catch (const std::exception& e)
            {
                std::cout << " MerkelMain::enterAsk Bad input " << std::endl;
            }   
        }
    }
    
    void MerkelMain::enterBid()
    {
        std::cout << "Make an bid - enter the amount: product,price, amount, eg  ETH/BTC,200,0.5" << std::endl;
        std::string input;
        std::getline(std::cin, input);
    
        std::vector<std::string> tokens = CSVReader::tokenise(input, ',');
        if (tokens.size() != 3)
        {
            std::cout << "MerkelMain::enterBid Bad input! " << input << std::endl;
        }
        else {
            try {
                OrderBookEntry obe = CSVReader::stringsToOBE(
                    tokens[1],
                    tokens[2], 
                    currentTime, 
                    tokens[0], 
                    OrderBookType::bid 
                );
                obe.username = "simuser";
    
                if (wallet.canFulfillOrder(obe))
                {
                    std::cout << "Wallet looks good. " << std::endl;
                    orderBook.insertOrder(obe);
                }
                else {
                    std::cout << "Wallet has insufficient funds . " << std::endl;
                }
            }catch (const std::exception& e)
            {
                std::cout << " MerkelMain::enterBid Bad input " << std::endl;
            }   
        }
    }
    
    void MerkelMain::printWallet()
    {
        std::cout << wallet.toString() << std::endl;
    }
            
    void MerkelMain::gotoNextTimeframe()
    {
        std::cout << "Going to next time frame. " << std::endl;
        for (std::string p : orderBook.getKnownProducts())
        {
            std::cout << "matching " << p << std::endl;
            std::vector<OrderBookEntry> sales =  orderBook.matchAsksToBids(p, currentTime);
            std::cout << "Sales: " << sales.size() << std::endl;
            for (OrderBookEntry& sale : sales)
            {
                std::cout << "Sale price: " << sale.price << " amount " << sale.amount << std::endl; 
                if (sale.username == "simuser")
                {
                    // update the wallet
                    wallet.processSale(sale);
                }
            }
            
        }
    
        currentTime = orderBook.getNextTime(currentTime);
    }
     
    std::string MerkelMain::getUserOption()
    {
        std::string userOption = "";
        std::string line;
        //std::cout << "Type in 1-6" << std::endl;
        std::getline(std::cin, line);
        try{
            userOption = std::stoi(line);
        }catch(const std::exception& e)
        {
            // 
        }
        std::cout << "You chose: " << userOption << std::endl;
        return userOption;
    }
    
    void printCurrentTime()
    {
        std::cout << "Current time is: " << currentTime << std::endl;
    
    }
    
    void MerkelMain::processUserOption(string userOption)
    {
        if (userOption == "help") 
        {
            printHelp();
        }
        if (userOption == "help cmd") 
        {
            printMarketStats();
        }
        if (userOption == "prod") 
        {
            enterAsk();
        }
        if (userOption == "min") 
        {
            enterBid();
        }
        if (userOption == "max") 
        {
            printWallet();
        }
        if (userOption == "avg") 
        {
            gotoNextTimeframe();
        } 
            if (userOption == "predict") 
        {
            enterAsk();
        }
        if (userOption == "time") 
        {
            enterBid();
        }
        if (userOption == "step") 
        {
            gotoNextTimeframe();
        }
          
    }
    
    1 回复  |  直到 1 周前
        1
  •  0
  •   Anoop Rana    1 周前

    你的 getUserOption() 函数返回一个 std::string

    input = getUserOption();
    

    在上述声明中 右侧 是一种 字符串 .另一方面 是一种 int .确实如此 在左侧和右侧表达式的类型中。因此出现了错误。

    你可以 解决 这可以通过更改变量的类型来实现 input 字符串 .因此,更换 int input; 作者:

    std::string input; //type changed to std::string