Cromulent
Cromulent

Reputation: 3818

No default constructor and errors out

Error received:

error: no matching function for call to ‘stout::SCGI::SCGI()’

Code:

#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?");
}

Question:

When I try and construct and object of type stout::SCGI::SCGI using the constructor stout::SCGI::SCGI(const std::vector<char>&) it fails in the above code with the error message shown at the top of this post.

It seems that before the constructor has finished it has already tried to call the default (empty) constructor for the scgi private member variable. I do not want an empty constructor on my class and have had to temporarily add one to fix this issue while I investigate it.

I've read other questions regarding this issue but can't seem to find a solution for this particular case.

If it matters I'm compiling the above code using G++ 4.9.2 on Arch Linux with the -std=c++14 flag.

Upvotes: 1

Views: 91

Answers (2)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

Your stout::SCGI type has no default constructor, yet you are not initialising this->scgi. Sure, you're assigning to it at the end of your constructor body, but that's not at all the same thing.

You need to initialise any members that are const or which cannot be default-constructed:

struct Foo
{
   stout::SCGI scgi;

   Foo()
     : scgi(/* ctor arguments here */)   // this is a "member initialisation list"
   {};
};

Also, the following is simply not valid syntax:

this->scgi = stout::SCGI scgi {netstring};

That lone scgi is clearly superfluous. At best, you want:

this->scgi = stout::SCGI{netstring};

However, once you're initialising this->scgi instead of waiting to assign to it, then this goes away completely.

Upvotes: 3

sfjac
sfjac

Reputation: 7294

What is scgi supposed to be here? I think you just want

this->scgi = stout::SCGI {netstring};

Upvotes: 1

Related Questions