收到的错误:
error: no matching function for call to ‘stout::SCGI::SCGI()’代码:
#include <gtest/gtest.h>
#include <vector>
#include "../../../../stout/cgi/scgi/scgi.hpp"
class SCGITest : public ::testing::Test
{
protected:
int string_length;
std::vector<char> netstring;
stout::SCGI scgi;
public:
SCGITest()
{
const char *c_netstring =
"70:CONTENT_LENGTH\00027\0"
"SCGI\0001\0"
"REQUEST_METHOD\0POST\0"
"REQUEST_URI\0/deepthought\0"
","
"What is the answer to life?";
string_length = 102;
for(int i = 0; i < string_length; ++i)
{
netstring.push_back(c_netstring[i]);
}
// SHOULD CALL stout::SCGI::SCGI(const std::vector<char>&)
this->scgi = stout::SCGI scgi {netstring};
scgi.init();
}
};
TEST_F(SCGITest, NetstringSize)
{
EXPECT_EQ(netstring.size(), string_length);
}
TEST_F(SCGITest, SCGILength)
{
EXPECT_EQ(scgi.get_length(), 70);
}
TEST_F(SCGITest, SCGIContentLength)
{
EXPECT_EQ(scgi.get_header("CONTENT_LENGTH"), "27");
}
TEST_F(SCGITest, SCGIVersion)
{
EXPECT_EQ(scgi.get_header("SCGI"), "1");
}
TEST_F(SCGITest, SCGIRequestMethod)
{
EXPECT_EQ(scgi.get_header("REQUEST_METHOD"), "POST");
}
TEST_F(SCGITest, SCGIRequestURI)
{
EXPECT_EQ(scgi.get_header("REQUEST_URI"), "/deepthought");
}
TEST_F(SCGITest, SCGIRequestBody)
{
EXPECT_EQ(scgi.get_request_body(), "What is the answer to life?");
}问题:
当我尝试使用构造函数stout::SCGI::SCGI构造和stout::SCGI::SCGI(const std::vector<char>&)类型的对象时,它在上面的代码中失败了,错误消息显示在文章的顶部。
似乎在构造函数完成之前,它已经尝试为scgi私有成员变量调用默认(空)构造函数。我不希望我的类上有一个空的构造函数,在我研究这个问题时,我不得不临时添加一个来解决这个问题。
我读过关于这个问题的其他问题,但似乎找不到解决这个问题的办法。
如果有关系,我将使用G++ 4.9.2在Arch上使用-std=c++14标志编译上述代码。
发布于 2014-11-29 18:05:49
您的stout::SCGI类型没有默认构造函数,但没有初始化this->scgi。当然,您在构造函数体的末尾分配给它,但这完全不是一回事。
您需要初始化任何属于const或不能默认构造的成员:
struct Foo
{
stout::SCGI scgi;
Foo()
: scgi(/* ctor arguments here */) // this is a "member initialisation list"
{};
};另外,以下是根本不有效的语法:
this->scgi = stout::SCGI scgi {netstring};孤独的scgi显然是多余的。充其量,你想:
this->scgi = stout::SCGI{netstring};然而,一旦您初始化this->scgi而不是等待分配给它,那么这个问题就完全消失了。
发布于 2014-11-29 18:00:55
scgi应该在这里做什么?我觉得你只是想
this->scgi = stout::SCGI {netstring};https://stackoverflow.com/questions/27204944
复制相似问题