简体   繁体   中英

C++ ERROR Console vs Desktop application visual studio

I am with a problem that I can't wrap my head around, been looking everywhere but to be honest I don't know what to look for. I am using Visual Studio 2019.

If I run the above code in a new project -> Windows Console Application everything goes smooth and working.
If I Do it on my current App it triggers the erros just by including the header file. I am posting here both headers, the one I include and the one that is triggering all the errors

However since I am trying to implement and work with a known library, I am trying to implement it on my Qt App ( Using Visual Studio also).

By me current experience that is not too much, it trigger the errors as soon as "include connection_result.h" is processed and from that window is where every error is coming.

It's weird because if I include the same headers on a new console application it works without any problem, is it something imcompatible? I even checked the enum class ( I was not full aware of that) and everything seems fine...

On my application as soon as I include the following error, I get errors. It says a lot of syntax error but I don't see any, and if that were the problem it would also trigger them on the console application right?

mavsdk.h

    #pragma once

#include <string>
#include <memory>
#include <vector>
#include <functional>

#include "system.h"
#include "connection_result.h"

namespace mavsdk {

class MavsdkImpl;
class System;

/**
 * @brief This is the main class of MAVSDK (a MAVLink API Library).

 * It is used to discover vehicles and manage active connections.
 *
 * An instance of this class must be created (first) in order to use the library.
 * The instance must be destroyed after use in order to break connections and release all resources.
 */
class Mavsdk {
public:
    /** @brief Default UDP bind IP (accepts any incoming connections). */
    static constexpr auto DEFAULT_UDP_BIND_IP = "0.0.0.0";
    /** @brief Default UDP bind port (same port as used by MAVROS). */
    static constexpr int DEFAULT_UDP_PORT = 14540;
    /** @brief Default TCP remote IP (localhost). */
    static constexpr auto DEFAULT_TCP_REMOTE_IP = "127.0.0.1";
    /** @brief Default TCP remote port. */
    static constexpr int DEFAULT_TCP_REMOTE_PORT = 5760;
    /** @brief Default serial baudrate. */
    static constexpr int DEFAULT_SERIAL_BAUDRATE = 57600;

    /**
     * @brief Constructor.
     */
    Mavsdk();

    /**
     * @brief Destructor.
     *
     * Disconnects all connected vehicles and releases all resources.
     */
    ~Mavsdk();

    /**
     * @brief Returns the version of MAVSDK.
     *
     * Note, you're not supposed to request the version too many times.
     *
     * @return A string containing the version.
     */
    std::string version() const;

    /**
     * @brief Adds Connection via URL
     *
     * Supports connection: Serial, TCP or UDP.
     * Connection URL format should be:
     * - UDP - udp://[Bind_host][:Bind_port]
     * - TCP - tcp://[Remote_host][:Remote_port]
     * - Serial - serial://Dev_Node[:Baudrate]
     *
     * @param connection_url connection URL string.
     * @return The result of adding the connection.
     */
    ConnectionResult add_any_connection(const std::string& connection_url);

    /**
     * @brief Adds a UDP connection to the specified port number.
     *
     * Any incoming connections are accepted (0.0.0.0).
     *
     * @param local_port The local UDP port to listen to (defaults to 14540, the same as MAVROS).
     * @return The result of adding the connection.
     */
    ConnectionResult add_udp_connection(int local_port = DEFAULT_UDP_PORT);

    /**
     * @brief Adds a UDP connection to the specified port number and local interface.
     *
     * To accept only local connections of the machine, use 127.0.0.1.
     * For any incoming connections, use 0.0.0.0.
     *
     * @param local_ip The local UDP IP address to listen to.
     * @param local_port The local UDP port to listen to (defaults to 14540, the same as MAVROS).
     * @return The result of adding the connection.
     */
    ConnectionResult
    add_udp_connection(const std::string& local_ip, int local_port = DEFAULT_UDP_PORT);

    /**
     * @brief Sets up instance to send heartbeats to the specified remote interface and port number.
     *
     * @param remote_ip The remote UDP IP address to report to.
     * @param remote_port The local UDP port to report to.
     * @return The result of operation.
     */
    ConnectionResult setup_udp_remote(const std::string& remote_ip, int remote_port);

    /**
     * @brief Adds a TCP connection with a specific port number on localhost.
     *
     * @param remote_port The TCP port to connect to (defaults to 5760).
     * @return The result of adding the connection.
     */
    ConnectionResult add_tcp_connection(int remote_port = DEFAULT_TCP_REMOTE_PORT);

    /**
     * @brief Adds a TCP connection with a specific IP address and port number.
     *
     * @param remote_ip Remote IP address to connect to.
     * @param remote_port The TCP port to connect to (defaults to 5760).
     * @return The result of adding the connection.
     */
    ConnectionResult
    add_tcp_connection(const std::string& remote_ip, int remote_port = DEFAULT_TCP_REMOTE_PORT);

    /**
     * @brief Adds a serial connection with a specific port (COM or UART dev node) and baudrate as
     * specified.
     *
     *
     * @param dev_path COM or UART dev node name/path (e.g. "/dev/ttyS0", or "COM3" on Windows).
     * @param baudrate Baudrate of the serial port (defaults to 57600).
     * @return The result of adding the connection.
     */
    ConnectionResult
    add_serial_connection(const std::string& dev_path, int baudrate = DEFAULT_SERIAL_BAUDRATE);

    /**
     * @brief Possible configurations.
     */
    enum class Configuration {
        Autopilot, /**< @brief SDK is used as an autopilot. */
        GroundStation, /**< @brief SDK is used as a ground station. */
        CompanionComputer /**< @brief SDK is used on a companion computer onboard the system (e.g.
                             drone). */
    };

    /**
     * @brief Set `Configuration` of SDK.
     *
     * The default configuration is `Configuration::GroundStation`
     * The configuration is used in order to set the MAVLink system ID, the
     * component ID, as well as the MAV_TYPE accordingly.
     *
     * @param configuration Configuration chosen.
     */
    void set_configuration(Configuration configuration);

    /**
     * @brief Get vector of system UUIDs.
     *
     * This returns a vector of the UUIDs of all systems that have been discovered.
     * If a system doesn't have a UUID then Mavsdk will instead use its MAVLink system ID
     * (range: 0..255).
     *
     * @return A vector containing the UUIDs.
     */
    std::vector<uint64_t> system_uuids() const;

    /**
     * @brief Get the first discovered system.
     *
     * This returns the first discovered system or a null system if no system has yet been found.
     *
     * @return A reference to a system.
     */
    System& system() const;

    /**
     * @brief Get the system with the specified UUID.
     *
     * This returns a system for a given UUID if such a system has been discovered and a null
     * system otherwise.
     *
     * @param uuid UUID of system to get.
     * @return A reference to the specified system.
     */
    System& system(uint64_t uuid) const;

    /**
     * @brief Callback type for discover and timeout notifications.
     *
     * @param uuid UUID of system (or MAVLink system ID for systems that don't have a UUID).
     */
    typedef std::function<void(uint64_t uuid)> event_callback_t;

    /**
     * @brief Returns `true` if exactly one system is currently connected.
     *
     * Connected means we are receiving heartbeats from this system.
     * It means the same as "discovered" and "not timed out".
     *
     * If multiple systems have connected, this will return `false`.
     *
     * @return `true` if exactly one system is connected.
     */
    bool is_connected() const;

    /**
     * @brief Returns `true` if a system is currently connected.
     *
     * Connected means we are receiving heartbeats from this system.
     * It means the same as "discovered" and "not timed out".
     *
     * @param uuid UUID of system to check.
     * @return `true` if system is connected.
     */
    bool is_connected(uint64_t uuid) const;

    /**
     * @brief Register callback for system discovery.
     *
     * This sets a callback that will be notified if a new system is discovered.
     *
     * If systems have been discovered before this callback is registered, they will be notified
     * at the time this callback is registered.
     *
     * @note Only one callback can be registered at a time. If this function is called several
     * times, previous callbacks will be overwritten.
     *
     * @param callback Callback to register.
     *
     */
    void register_on_discover(event_callback_t callback);

    /**
     * @brief Register callback for system timeout.
     *
     * This sets a callback that will be notified if no heartbeat of the system has been received
     * in 3 seconds.
     *
     * @note Only one callback can be registered at a time. If this function is called several
     * times, previous callbacks will be overwritten.
     *
     * @param callback Callback to register.
     */
    void register_on_timeout(event_callback_t callback);

private:
    /* @private. */
    std::unique_ptr<MavsdkImpl> _impl;

    // Non-copyable
    Mavsdk(const Mavsdk&) = delete;
    const Mavsdk& operator=(const Mavsdk&) = delete;
};

} // namespace mavsdk

connection_result.h

#pragma once

/**
 * @brief Namespace for all mavsdk types.
 */
namespace mavsdk {

/**
 * @brief Result type returned when adding a connection.
 *
 * **Note**: Mavsdk does not throw exceptions. Instead a result of this type will be
 * returned when you add a connection: add_udp_connection().
 */
enum class ConnectionResult {
    SUCCESS = 0, /**< @brief %Connection succeeded. */
    TIMEOUT, /**< @brief %Connection timed out. */
    SOCKET_ERROR, /**< @brief Socket error. */
    BIND_ERROR, /**< @brief Bind error. */
    SOCKET_CONNECTION_ERROR, /**< @brief Socket connection error. */
    CONNECTION_ERROR, /**< @brief %Connection error. */
    NOT_IMPLEMENTED, /**< @brief %Connection type not implemented. */
    SYSTEM_NOT_CONNECTED, /**< @brief No system is connected. */
    SYSTEM_BUSY, /**< @brief %System is busy. */
    COMMAND_DENIED, /**< @brief Command is denied. */
    DESTINATION_IP_UNKNOWN, /**< @brief %Connection IP is unknown. */
    CONNECTIONS_EXHAUSTED, /**< @brief %Connections exhausted. */
    CONNECTION_URL_INVALID, /**< @brief URL invalid. */
    BAUDRATE_UNKNOWN /**< @brief Baudrate unknown. */
};

/**
 * @brief Returns a human-readable English string for a ConnectionResult.
 *
 * @param result The enum value for which a human readable string is required.
 * @return Human readable string for the ConnectionResult.
 */
inline const char* connection_result_str(const ConnectionResult result)
{
    switch (result) {
        case ConnectionResult::SUCCESS:
            return "Success";
        case ConnectionResult::TIMEOUT:
            return "Timeout";
        case ConnectionResult::SOCKET_ERROR:
            return "Socket error";
        case ConnectionResult::BIND_ERROR:
            return "Bind error";
        case ConnectionResult::SOCKET_CONNECTION_ERROR:
            return "Socket connection error";
        case ConnectionResult::CONNECTION_ERROR:
            return "Connection error";
        case ConnectionResult::NOT_IMPLEMENTED:
            return "Not implemented";
        case ConnectionResult::SYSTEM_NOT_CONNECTED:
            return "System not connected";
        case ConnectionResult::SYSTEM_BUSY:
            return "System busy";
        case ConnectionResult::COMMAND_DENIED:
            return "Command denied";
        case ConnectionResult::DESTINATION_IP_UNKNOWN:
            return "Destination IP unknown";
        case ConnectionResult::CONNECTIONS_EXHAUSTED:
            return "Connections exhausted";
        case ConnectionResult::CONNECTION_URL_INVALID:
            return "Invalid connection URL";
        case ConnectionResult::BAUDRATE_UNKNOWN:
            return "Baudrate unknown";
        default:
            return "Unknown";
    }
}

} // namespace mavsdk

Error Logs from Visual Studio:

1>------ Build started: Project: Drones_QT_teste, Configuration: Debug x64 ------
1>AssignDronesWindow.cpp
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(17,5): error C2143: syntax error: missing '}' before '('
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(17,5): error C2059: syntax error: '-'
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(29,1): error C2143: syntax error: missing ';' before '}'
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(37,65): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(37,65): error C2146: syntax error: missing ')' before identifier 'result'
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(37,71): error C3646: 'result': unknown override specifier
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(37,71): error C2059: syntax error: ')'
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(39,13): error C2065: 'result': undeclared identifier
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(39,21): error C2050: switch expression not integral
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(40,39): error C2653: 'ConnectionResult': is not a class or namespace name
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(40,32): error C2065: 'SUCCESS': undeclared identifier
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(42,39): error C2653: 'ConnectionResult': is not a class or namespace name
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(42,32): error C2065: 'TIMEOUT': undeclared identifier
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(44,32): error C2589: '(': illegal token on right side of '::'
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(44,32): error C2062: type 'unknown-type' unexpected
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(44,32): error C2059: syntax error: ')'
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(46,42): error C2653: 'ConnectionResult': is not a class or namespace name
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(46,32): error C2065: 'BIND_ERROR': undeclared identifier
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(48,55): error C2653: 'ConnectionResult': is not a class or namespace name
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(48,32): error C2065: 'SOCKET_CONNECTION_ERROR': undeclared identifier
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(50,48): error C2653: 'ConnectionResult': is not a class or namespace name
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(50,32): error C2065: 'CONNECTION_ERROR': undeclared identifier
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(52,47): error C2653: 'ConnectionResult': is not a class or namespace name
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(52,32): error C2065: 'NOT_IMPLEMENTED': undeclared identifier
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(54,52): error C2653: 'ConnectionResult': is not a class or namespace name
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(54,32): error C2065: 'SYSTEM_NOT_CONNECTED': undeclared identifier
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(56,43): error C2653: 'ConnectionResult': is not a class or namespace name
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(56,32): error C2065: 'SYSTEM_BUSY': undeclared identifier
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(58,46): error C2653: 'ConnectionResult': is not a class or namespace name
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(58,32): error C2065: 'COMMAND_DENIED': undeclared identifier
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(60,54): error C2653: 'ConnectionResult': is not a class or namespace name
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(60,32): error C2065: 'DESTINATION_IP_UNKNOWN': undeclared identifier
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(62,53): error C2653: 'ConnectionResult': is not a class or namespace name
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(62,32): error C2065: 'CONNECTIONS_EXHAUSTED': undeclared identifier
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(64,54): error C2653: 'ConnectionResult': is not a class or namespace name
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(64,32): error C2065: 'CONNECTION_URL_INVALID': undeclared identifier
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(66,48): error C2653: 'ConnectionResult': is not a class or namespace name
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(66,32): error C2065: 'BAUDRATE_UNKNOWN': undeclared identifier
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(40,1): error C2051: case expression not constant
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(42,1): error C2051: case expression not constant
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(46,1): error C2051: case expression not constant
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(48,1): error C2051: case expression not constant
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(50,1): error C2051: case expression not constant
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(52,1): error C2051: case expression not constant
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(54,1): error C2051: case expression not constant
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(56,1): error C2051: case expression not constant
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(58,1): error C2051: case expression not constant
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(60,1): error C2051: case expression not constant
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(62,1): error C2051: case expression not constant
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(64,1): error C2051: case expression not constant
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(66,1): error C2051: case expression not constant
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(73,1): error C2059: syntax error: '}'
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\connection_result.h(73,1): error C2143: syntax error: missing ';' before '}'
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\mavsdk.h(11,18): error C2143: syntax error: missing ';' before '{'
1>C:\Program Files (x86)\mavsdk_superbuild\include\mavsdk\mavsdk.h(11,18): error C2447: '{': missing function header (old-style formal list?)
1>C:\Users\Backlight_AG\Desktop\Drones_QT_teste\Drones_QT_teste\AssignDronesWindow.cpp(159,5): error C2065: 'Mavsdk': undeclared identifier
1>C:\Users\Backlight_AG\Desktop\Drones_QT_teste\Drones_QT_teste\AssignDronesWindow.cpp(159,12): error C2146: syntax error: missing ';' before identifier 'dc'
1>C:\Users\Backlight_AG\Desktop\Drones_QT_teste\Drones_QT_teste\AssignDronesWindow.cpp(159,12): error C2065: 'dc': undeclared identifier
1>Done building project "Drones_QT_teste.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

FIXED

By not knowing what to do I redefined the name "SOC_ERROR" to "SOC_ERROR2" in the enum class it does the same and the result is the same. However I'am not sure what was causing the problem.

My theory: On a new C++ project console application without QT everything worked. On this Project where I have qt being used, SOCK_ERROR was already being defined in "winsock.h", I was not importing from there but the librarys I am importing through QT may include that. and then, causing the error

If anybody has insight on this I would appreciate, even if the questions is solved.

Thanks!

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