Yet Another "No Appropriate Default Constructor Available" thread

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//AccList.h

#pragma once
#include <iostream>
#include <string>
#include "HoldList.h"
using namespace 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;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//HoldList.h

#pragma once
#include <iostream>
#include <string>
#include "StockList.h"
#include "AccList.h"

using namespace std;

struct AccountNode;

struct Holding
{
	string symbol;
	int held;
};

struct HoldNode
{
	Holding data;
	HoldNode *next;
};

class HoldList
{
public:
	HoldList();
	HoldList(const HoldList&);
	~HoldList();

	friend class AccList;
	
	//string Symbol;

	void addHolding(StockList, AccountNode*&);
	void addHolding(string, int, StockList, AccountNode*&);
	void addHolding(HoldNode*, StockList, AccountNode*&);

	void deleteHolding(StockList, AccountNode*&);
	void deleteHolding(string, int, StockList, AccountNode*&);
	void deleteHolding(HoldNode*, StockList, AccountNode*&);

	void input();
	void output();

	void list();

	double totalValue(StockList);

	HoldNode* search(string);

private:
	HoldNode *root;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//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(){

}
Last edited on
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.
Last edited on
Topic archived. No new replies allowed.