在 C++ 函数中返回对类型会阻止编译
Returning a pair type in C++ function prevents complation
我有一个 C++ 函数 header:
int DECLSPEC __cdecl HandleUpdate(uint8_t* MessageBuffer, uint32_t MessageLength );
但是,我不希望这个功能return一个int
,我现在需要它return一个std::pair<uint8_t*, uint32_t>*
。
在 .h
和 [=18] 中仅从字面上更改 return 类型 (并在函数中添加 return 值) =] 文件抛出这些错误,尽管当 return 类型是标准类型(int
、bool
、void
)时代码编译正确:
//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
代码当前状态:
//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。
我定义 return 类型的方式有问题吗?
非常感谢所有建议。
MINIMUM REPRODUCIBLE EXAMPLE
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)
。并放在行首。
我有一个 C++ 函数 header:
int DECLSPEC __cdecl HandleUpdate(uint8_t* MessageBuffer, uint32_t MessageLength );
但是,我不希望这个功能return一个int
,我现在需要它return一个std::pair<uint8_t*, uint32_t>*
。
在 .h
和 [=18] 中仅从字面上更改 return 类型 (并在函数中添加 return 值) =] 文件抛出这些错误,尽管当 return 类型是标准类型(int
、bool
、void
)时代码编译正确:
//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
代码当前状态:
//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。
我定义 return 类型的方式有问题吗?
非常感谢所有建议。
MINIMUM REPRODUCIBLE EXAMPLE
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)
。并放在行首。