繁体   English   中英

在 C++ function 中返回一对类型可防止编译

[英]Returning a pair type in C++ function prevents complation

我在 C++ 中有一个 function 和以下 header:

int DECLSPEC __cdecl HandleUpdate(uint8_t* MessageBuffer, uint32_t MessageLength );

但是,我不希望这个 function 返回一个int ,我现在需要它返回一个std::pair<uint8_t*, uint32_t>*

仅从字面上更改.h.cpp文件中的返回类型(并在函数中添加返回值)会引发这些错误,尽管当返回类型为标准类型( intboolvoid )时代码编译正确:

//all of these errors pertain to the code snippets I have made available

error C2059: syntax error: '__declspec(dllexport)'                 //on the declaration in the header file
error C2238: unexpected token(s) preceding ';'                     //on the declaration in the header file
error C2059: syntax error: '__declspec(dllexport)'                 // on `std` in the return type in the .cpp file
error C2039: 'HandleUpdate': is not a member of 'EACServer'        //in the .cpp file
error C2143: syntax error: missing ';' before '{'                  //on opening curly bracket of .cpp file function
error C2447: '{': missing function header (old-style formal list?) //on opening curly bracket of .cpp file function

当前state的代码:

//HEADER
std::pair<uint8_t*, uint32_t>* DECLSPEC __cdecl HandleUpdate(uint8_t* MessageBuffer, uint32_t MessageLength) const;

//CPP
std::pair<uint8_t*, uint32_t>* DECLSPEC __cdecl EACServer::HandleUpdate(uint8_t* MessageBuffer, uint32_t MessageLength)
{
    //do some cool stuff
    std::pair<uint8_t*, uint32_t> tmp( MessageBuffer, MessageLength );
    this->messageArray[this->messagesInArray] = tmp;
    this->messagesInArray += 1;
    return this->messageArray;
}

我正在使用 VS 2017 并在 Windows 10 x64 机器上编译为 Release x64。

我定义返回类型的方式有问题吗?

非常感谢所有建议。

最小可重现示例

Header 档案

// Foo.h
#pragma once
#ifndef Foo_H
#define Foo_H

#include <stdint.h>
#include <string>
#include <utility>

class Foo:
{
public:
    std::pair<uint8_t*, uint32_t>* DECLSPEC __cdecl HandleUpdate( uint8_t* 
    MessageBuffer,uint32_t MessageLength );
private:
    mutable std::pair<uint8_t*, uint32_t>* messageArray;
    mutable int messagesInArray;
}

.CPP 文件:

#include "stdafx.h"
#include <iostream>

#include "Foo.h"

typedef int( __cdecl* MYPROC )( LPWSTR );

std::pair<uint8_t*, uint32_t>* DECLSPEC __cdecl EACServer::HandleUpdate(                                               uint8_t* MessageBuffer, uint32_t MessageLength )
{
    std::pair<uint8_t*, uint32_t> tmp( MessageBuffer, MessageLength );
    this->messageArray[this->messagesInArray] = tmp;
    this->messagesInArray += 1;
return this->messageArray;
}

int main()
{
Foo bar = Foo();
uint8_t* MessageBuffer = 0;
uint32_t MessageLength = 0;
std::pair<uint8_t*, uint32_t>* result = bar.HandleUpdate(MessageBuffer, MessageLength);
}

.cpp中的定义中删除__decslspec(dllexport) 并将其放在行的开头。

使用 __declspec(dllexport) 从 DLL 导出

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM