I'm working on a homework assignment for a C++ course where I chose to use Linked Lists instead of arrays (both for extra credit and as a learning experience). I'll go to it:
I have 2 classes at work, here: HoldList and AccList. In AccList, there is a struct that has a HoldList* as a data member, as you'll see below. I DO have a default constructor. The most frustrating issue with this problem for me is that sometimes it will compile, sometimes it won't. I've gotten it to compile by shifting a space or a dereference operator, and then later, with absolutely 0 (I've confirmed, 0) changes, it will return the error on compile.
The whole thing (cut down to relevant parts) looks like this:
error C2512: 'HoldList' : no appropriate default constructor available
//AccList.h
#pragma once
#include <iostream>
#include <string>
#include "HoldList.h"
usingnamespace std;
class HoldList; //I have included HoldList.h in the AccList.cpp file, as well.
//Without this line, the whole thing fails spectacularly,
//and I have had it work perfectly well with this line.
struct AccountNode {
string id;
HoldList* holdings = new HoldList; //This is the line that returns the error.
//I've reduced it to HoldList* holdings;, I've tried to split it into two lines.
//Nothing I do seems to have any consistent meaning or effect as to
//whether or not it compiles.
double balance;
AccountNode *next;
};
//HoldList.cpp
#include "HoldList.h"
HoldList::HoldList()
{
root = new HoldNode; //fails with or without this line
root = NULL;
}
HoldList::HoldList(const HoldList& h1)
{
root = h1.root;
}
HoldList::~HoldList(){
}
I've solved my own issue. If you look at the top of HoldList.h, it imports AccList.h.
Visual Studio was building HoldList.h first, which called AccList.h and created a circular reference. When it resolved it, it hadn't yet made the HoldList class, leaving AccList.h with no default constructor to work with.
I removed the #include "AccList.h" from HoldList.h and it moved it to HoldList.cpp, where it is actually needed, and now everything works great.
EDIT: Depending on how Visual Studio 2013 works (since I'm not sure), this may also explain why sometimes it would compile, and sometimes it wouldn't. Tiny minor edits may have affected the build order.