简体   繁体   English

C++ 错误控制台与桌面应用程序 Visual Studio

[英]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.我正在使用 Visual Studio 2019。

If I run the above code in a new project -> Windows Console Application everything goes smooth and working.如果我在新项目中运行上述代码 -> Windows 控制台应用程序,一切都会顺利进行。
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).但是,由于我正在尝试实施和使用已知库,因此我正在尝试在我的 Qt 应用程序上实施它(也使用 Visual Studio)。

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.根据我目前的经验,它不会太多,一旦处理“include connection_result.h”就会触发错误,并且从那个窗口开始每个错误都会出现。

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 mavsdk文件

    #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连接结果.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:来自 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.由于不知道该怎么做,我在枚举类中将名称“SOC_ERROR”重新定义为“SOC_ERROR2”,结果相同。 However I'am not sure what was causing the problem.但是我不确定是什么导致了这个问题。

My theory: On a new C++ project console application without QT everything worked.我的理论:在没有 QT 的新 C++ 项目控制台应用程序上一切正常。 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.在我使用 qt 的这个项目中,SOCK_ERROR 已经在“winsock.h”中定义,我没有从那里导入,但我通过 QT 导入的库可能包含它。 and then, causing the error然后,导致错误

If anybody has insight on this I would appreciate, even if the questions is solved.如果有人对此有见解,我将不胜感激,即使问题已解决。

Thanks!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在 Visual Studio 上用 C++ 创建桌面应用程序 - Trying to create a desktop application in C++ on visual studio Visual Studio C ++控制台应用程序无法运行 - Visual Studio C++ Console Application Won't Run 在Visual Studio 2012中对C ++控制台应用程序进行单元测试 - Unit testing c++ console application in Visual Studio 2012 Visual Studio 2022 for mac 无法创建 c++ 控制台应用程序 - visual studio 2022 for mac cannot create c++ console application 在macOS的Visual Studio中创建C ++控制台应用程序 - Creating a C++ console application in Visual Studio on macOS Visual Studio 2012 C ++控制台应用程序立即退出 - Visual Studio 2012 C++ console application exits Immediately Visual Studio C ++ 2010如何将ICON添加到控制台应用程序 - Visual Studio C++ 2010 How to add an ICON to a console application 无法在Visual Studio 2013中创建C ++控制台应用程序 - Can't create C++ console application in Visual Studio 2013 C ++-Qt-Visual Studio 2010-具有GUI和控制台的应用程序 - C++ - Qt - Visual Studio 2010 - application with both gui and console Visual Studio 2019 C++ 桌面应用程序; 如何制作一个孩子 window? - Visual Studio 2019 C++ Desktop application; How do I make a child window?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM