3

I would like to build a struct Packet that contains list of Headers. The relevant code I use:

//Packet.h
#include <list>

using namespace std;

struct Header {
    Header();
    bool Valid;
    long unsigned DestAddr:48;
};

struct Packet_t {
    Packet_t();
    list<Header> Headers;
};

Now I try to build the constructor for Packet_t that will initialize the Headers list to include only one header - FirstHeader:

//Packet.cpp
#include "Packet.h"

Header::Header() {
    Valid = false;
    DestAddr = 0;
};

Packet_t::Packet_t(){
    ValidPacket = false;
    Header FirstHeader(); //here I try to initialize the first Header using its constructor
    Headers.push_front(FirstHeader);
};

The error I get:

Packet.cpp: error: no matching function for call to 'std::list >::push_front(Header (&)())'

Really appreciate any help

Halona
  • 1,475
  • 1
  • 15
  • 26
  • 3
    `Header FirstHeader();` declared a function, not an object. Just use `Header FirstHeader;` – WhozCraig Mar 04 '14 at 11:54
  • WhozCraig, but would it create an "initialized" Header object (as I defined the Header initialization in its constructor) ? Thank you – Halona Mar 04 '14 at 11:56
  • 3
    Also, stop putting `using namespace std` in header files. It's naughty..! – Sean Mar 04 '14 at 11:57
  • 4
    @Halona Yes, the default constructor (yours) is fired. Sadly this question is asked what seems like *daily*, but because everyone uses different names for different variables and classes, its difficult to link the *hundreds* of duplicates that already exist covering it. Its not an uncommon error, so don't feel you made some dreadful mistake. – WhozCraig Mar 04 '14 at 11:57
  • Sean, Why (i'm very new to c++) ? It saves me from writing "std::" every tine, doesn't it? – Halona Mar 04 '14 at 12:03
  • 3
    @Halona Regarding `using namespace std;`, [that question I *can* link a reference to](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). This [answer](http://stackoverflow.com/a/1452759/1322972) in particular is applicable to you. – WhozCraig Mar 04 '14 at 12:04
  • 1
    Avoid using std::list unless you have a reason for it, otherwise use std::vector. And by "reason for it" I mean because the profiler told you. – Átila Neves Mar 04 '14 at 12:45

1 Answers1

7

This is a function declaration:

Header FirstHeader();  // function FirstHeader, returns a Header

What you need is

Headers.push_front(Header());

or

Headers.emplace_front(); // default constructs FirstHeader object in place

or, if you need an instance to work on before pushing,

Header FirstHeader;
// ... do stuff to Header

Headers.push_front(FirstHeader);

Alternatively, use the constructor initialization list to initialize the list with one element:

Packet_t::Packet_t() : Headers(1) // or Headers{Header(args....) if need args
{

}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • Ah, the most vexing parse rears its ugly head yet again. – Átila Neves Mar 04 '14 at 12:42
  • 2
    @ÁtilaNeves this isn't an MVP. Its simply an declarative misstep. An MVP trips when attempting something similar, but actually providing a construction parameter which *also* happens to be intended as an immediate constructed temp: For example, consider: `Something obj(Thing())`. – WhozCraig Mar 04 '14 at 18:04
  • @WhozCraig Right, it is the "mildly vexing parse" MVP. This one doesn't make me do a double-take. – juanchopanza Mar 04 '14 at 18:44