小编And*_*Pro的帖子

SetupAPI(setupdi 函数)不链接

我想要一个函数来枚举 Windows 上的串行 (COM) 端口。为此,我主要从http://www.codeguru.com/cpp/wp/system/hardwareinformation/article.php/c5721/Determining-What-Serial-Ports-Are-Available-on-a-Windows-复制代码机器.htm

在头文件中:

#include "SerialPort.h"
#include <list>
#include <objbase.h>
#include <initguid.h>
#include <Setupapi.h>
typedef std::list<SerialPort> PortList;
class SerialConnection
{
private:
static PortList availible_ports;
public:
static void enumerateSerialPorts(bool);
static const PortList& getPortList(){ return availible_ports; }
}
Run Code Online (Sandbox Code Playgroud)

执行:

void SerialConnection::enumerateSerialPorts(bool check)
{
    availible_ports.clear();
    CString strErr;
    // Create a device information set that will be the container for 
    // the device interfaces.
    GUID *guidDev = (GUID*)&GUID_CLASS_COMPORT;

    HDEVINFO hDevInfo = INVALID_HANDLE_VALUE;
    SP_DEVICE_INTERFACE_DETAIL_DATA *pDetData = NULL;

    try {
        hDevInfo …
Run Code Online (Sandbox Code Playgroud)

windows linker serial-port setupapi visual-c++

4
推荐指数
1
解决办法
6787
查看次数

使用std :: thread从派生类调用overriden方法

我想尝试这样的事情:

int main()
{
    class Base
    {
    public:
        Base() = default;
        virtual void print() = 0;
        void CallThread()
        {
            std::thread(&Base::print, *this);
        };
    };

    class derived1 : public Base
    {
    public:
        derived1() = default;
        void print() { printf("this is derived1\n"); }

    };
    class derived2 : public Base
    {
    public:
        derived2() = default;
        void print() { printf("this is derived2\n"); }

    };
    Base* ptr;
    ptr = new derived1();
    ptr->CallThread();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我想要的结果是:

这是派生的1.

问题是它甚至不会编译.我正在使用VisualStudio 2013.

错误是:

错误2错误C3640:'main :: Base :: [thunk]:__ thiscall …

c++ polymorphism c++11

3
推荐指数
1
解决办法
704
查看次数

用什么来存储Unicode(UTF-16)字符串?(C++ 11)

我根据C++ 11带来的创新,即uchar16_t/u16string来提出这个问题.

我写了一个应该有多语言支持的应用程序.根据我的计划,本地化字符串将以XML格式存储为UTF-16,并使用pugixml进行检索.这些字符串既可用于GUI,也可用于生成计算结果的HTML报告.由于我已经将wchar_t/wstring理解为不赞成使用新的u16string,因此我计划使用u16string在程序中存储语言字符串.但是既然pugixml和MFC的CString都使用wchar_t作为Unicode的下划线存储类型,我现在应该忘记u16string而是直接使用wstring吗?

语言可移植性至关重要,平台可移植性无关紧要.

我使用MVS 2013和英特尔编译器.

c++ unicode c++11

2
推荐指数
1
解决办法
940
查看次数

c ++:尝试使用const成员构造一个类

考虑以下课程:

class MyClass
{
private:
    const unsigned int num;//num identifies the object. needs to be const
    unsigned int checkNum(unsigned int);// verifies that num has a valid value

public:
    MyClass(unsigned int n): num(checkNum(n)) {}
};

unsigned int MyClass:checkNum(unsigned int n)
{
    if (some_condition)
        throw std::invalid_argument("Invalid number");
    return n;
}
Run Code Online (Sandbox Code Playgroud)

难点在于,try由于范围检查,必须在块内构造对象:

int main()
{
    try {
        MyClass mc(1000);
    }
    catch (std::invalid_argument &ia)
    {
        std::cout << ia.what();
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

问题是mctry块之外是不可用的.

可能的解决方案:

  1. try在使用的整个范围内扩展块mc.在许多情况下不实用. …

c++ constructor const exception try-catch

1
推荐指数
1
解决办法
86
查看次数