I get the following error generated from the cstor of the StartMeUp class.
no matching function for call to BoxMaker::BoxMaker
note candidates are: BoxMaker::BoxMaker(const BoxMaker&)
note BoxMaker::BoxMaker(std::vector<Token, std::allocator<Token> >)
As far as I understand things, I am not making a call to the BoxMaker class from the cstor of StartMeUp. After the error I included BoxMaker.h just in case. It did not help anything.
Below is the StartMeUp cstor and header file as well as the .h file for BoxMaker. Thanks in advance for all the help. Also, I am very open to general recommendations about "good practice" and design. I am very much still on the steep part of the learning curve.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//STARTMEUP CONSTRUCTOR
#include "StartMeUp.h"
#include "Token_stream.h"
#include "BoxMaker.h"
#include <vector>
using std::vector;
StartMeUp::StartMeUp()
{
//DEBUGGER FLAGS THIS BLANK LINE WITH THE ERROR
LanCalc();
vector<Token> LocalHosts;
LocalHosts = GetHosts();
LocalHosts = GetTestTemplate(LocalHosts);
int MaximumMask = CalcMaxSNM(Hosts);
BaseIP = GetOctets2( Hosts, MaximumMask);
NetworkBits = ConvertToNetworkBits( BaseIP);
}
You are trying to call BoxMaker's default constructor (see line 26, just above) but by declaring a user-defined
constructor for BoxMaker you no longer get a default constructor provided by the compiler.
Example:
1 2 3 4 5 6 7 8 9 10 11
class Foo {
private:
std::string aString;
public:
Foo( const std::string& str )
// Compiler tries to call std::string::string() constructor for aString here
{
aString = str;
}
};
The above works since std::string() has a default constructor. You can specify to call a different
constructor this way:
1 2 3 4 5 6 7 8 9 10
class Foo {
private:
std::string aString;
public:
Foo( const std::string& str )
: aString( str ) // Specify to call std::string copy constructor instead to copy str to aString
{
}
};
Read up on initializer lists (the part after the colon) if you are not familiar.
I am familiar with member initializer lists. But are you saying that they have to be used in the declaration of the user defined constructor (my .h file) as well as the definition of the user defined cstor as well (my related .cpp file) ??
but by declaring a user-defined
constructor for BoxMaker you no longer get a default constructor provided by the compiler.
This I understand, but since I wrote my own constructor, why would this matter?
#include "BoxMaker.h"
#include <vector>
BoxMaker::BoxMaker(vector<Token> Hosts_in):
theHosts(Hosts_in)
{
int numOfSubnets;
int h;
int lasttoken = theHosts.size()-1;
if (theHosts[lasttoken].kind == 'c' ||
theHosts[lasttoken].kind == 'C' ||
theHosts[lasttoken].kind == 'd' ||
theHosts[lasttoken].kind == 'D' )
h = 1;
numOfSubnets = theHosts.size()+h; //remember that template token in on the back
for (int i = 0; i<numOfSubnets; i++)
{
myBoxes.push_back(new SimpleBox(theHosts));
}
}
BoxMaker is getting called from the header file. By I don't specify the proper parameters in the header file for the BoxMaker class....(probably because I changed them after the original coding)...
This is what happens when you are trying to build onto something you already wrote....It has been troublesome.
class Foo {
private:
std::string str;
std::vector<int> vec;
public:
Foo( constchar* s, size_t n, int v );
};
and the following implementation of the constructor:
1 2 3 4 5 6 7 8
Foo::Foo( constchar* s, size_t n, int v )
// The C++ standard guarantees you that by the time the first line of your constructor runs, all data
// members have been fully constructed. Because the compiler doesn't know any better, it attempts
// to call the default constructor on all data members. This results in a call to string's default constructor
// (to initialize str) and to vector<>'s default constructor (to initialize vec) in that order.
{
std::cout << str << std::endl; // This will output nothing, since str is default constructed to the empty string.
}
Now consider this implementation:
1 2 3 4 5 6
Foo::Foo( constchar* s, size_t n, int v )
: str( s ) // str is now constructed using string::string( const char* ) constructor
, vec( n, v ) // vec is now constructed using vector<int>::vector<int>( size_t n, int v = v() ) constructor
{
std::cout << str << std::endl; // This will output whatever the c-string pointed to by "s" was
}