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

时间:2019-03-20 16:35:39

标签: c++ boost visual-studio-code boost-asio ncurses

我刚刚开始为任务编写服务器和客户端,但是无法编译我的客户端代码。服务器仅使用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.cc:

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

1 个答案:

答案 0 :(得分:1)

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