尝试使用boost和ncurses库编译程序时出错

问题描述 投票:0回答:1

我刚刚开始为作业编写服务器和客户端,但无法编译我的客户端代码。服务器只使用boost库编译就好了但在我的客户端代码中我使用的是boost和ncurses。

我在尝试编译时使用g++ battleship_client.cc -lboost_system -lncurses

我用Google搜索过疯狂,但我还没有找到解决方案。我还应该注意到我在Mac上并且我正在使用vscode。我已将includePath更改为如下所示:

"includePath": [
            "${workspaceFolder}/**",
            "/usr/local/Cellar/boost/1.68.0_1/",
            "/usr/local/Cellar/ncurses/6.1/"
        ],

battleship_client.曹操:

#include <iostream>
#include <string>
#include <vector>
#include <ncurses.h>
#include <boost/asio.hpp>

using namespace std;
using boost::asio::ip::tcp;


void draw_top_matrix(vector<vector<int> > &board, int cur_row, int cur_col) {
    for (int j=0;j<4;j++) {
        move(0,2*j);
        printw("+-");
    }

    move(0,2*4);
    printw("+");

    for (int i=0;i<4;i++) {
        for (int j=0;j<4;j++) {
            move(2*i+1,2*j);
            printw("|");
            move(2*i+1,2*j+1);

            if (board[i][j] == 0) {
                printw(" ");
            } 
            else {
                printw("X");
            }
        }

        move(2*i+1,2*4);
        printw("|");

        for (int j=0;j<4;j++) {
            move(2*i+2,2*j);
            printw("+-");
        }

        move(2*i+2,2*4);
        printw("+");
    }

    move(2*cur_row+1,2*cur_col+1);
}

void init(vector<vector<int> > &board) {
    int rows;
    int cols;
    int cur_row = 0;
    int cur_col = 0;

    for(int i = 0; i < 4; i++) {
        vector<int> temp;
        for(int j = 0; j < 4; j++) {
            temp.push_back(0);
        }
        board.push_back(temp);
    }

    initscr();
    clear();

    getmaxyx(stdscr, rows, cols);

    cbreak();
    keypad(stdscr, TRUE);
    refresh();

    draw_top_matrix(board, 0, 0);
    endwin();
}

int main(int argc, char *argv[]) {
    int port = atoi(argv[3]);
    vector<vector<int> > board;
    boost::asio::io_service my_service;
    tcp::resolver resolver(my_service);

    tcp::socket socket(my_service);
    socket.connect(tcp::endpoint(boost::asio::ip::address::from_string(argv[2]), port));

    init(board);

    return 0;
}

我得到的错误:

    In file included from battleship_client.cc:4:0:
/usr/local/include/boost/asio/basic_socket_streambuf.hpp:595:7: error: 'stdscr' is not a type
   int timeout() const
       ^
/usr/local/include/boost/asio/basic_socket_streambuf.hpp:595:7: error: expected identifier before ')' token
   int timeout() const
       ^
/usr/local/include/boost/asio/basic_socket_streambuf.hpp: In member function 'std::basic_streambuf<char>::int_type boost::asio::basic_socket_streambuf<Protocol, Clock, WaitTraits>::underflow()':
/usr/local/include/boost/asio/basic_socket_streambuf.hpp:479:42: error: expected primary-expression before ')' token
             socket().native_handle(), 0, timeout(), ec_) < 0)
                                          ^
/usr/local/include/boost/asio/basic_socket_streambuf.hpp: In member function 'std::basic_streambuf<char>::int_type boost::asio::basic_socket_streambuf<Protocol, Clock, WaitTraits>::overflow(std::basic_streambuf<char>::int_type)':
/usr/local/include/boost/asio/basic_socket_streambuf.hpp:538:42: error: expected primary-expression before ')' token
             socket().native_handle(), 0, timeout(), ec_) < 0)
                                          ^
/usr/local/include/boost/asio/basic_socket_streambuf.hpp: In member function 'void boost::asio::basic_socket_streambuf<Protocol, Clock, WaitTraits>::connect_to_endpoints(EndpointIterator, EndpointIterator)':
/usr/local/include/boost/asio/basic_socket_streambuf.hpp:656:39: error: expected primary-expression before ')' token
             socket().native_handle(), timeout(), ec_) < 0)
c++ boost visual-studio-code boost-asio ncurses
1个回答
1
投票

ncurses.h将timeout定义为预处理器宏。尝试添加NCURSES_NOMACROS定义

© www.soinside.com 2019 - 2024. All rights reserved.