Map of object

Hi.
Im new to c++, and have currently a problem mapping my object:

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

#include "dag.h"
#include "dagnode.h"
#include <limits>
#include <map>
using std::cout;
using std::endl;
using std::map;

using namespace std;


map<int, dagnode> dagnodeMap;
int idCounter = 0;

/******************************
 * At construction is root made
 ******************************/
dag::dag() {
	makeVersion({}, 0);
}

/*******************************************
 * Makes a new version node,
 * and connect it bidirected with ancestors
 * if ancestor i null, then it is a root
 *******************************************/
void dag::makeVersion(dagnode *a, int numberOfAncestors){
	idCounter = idCounter+1;
	int id = idCounter;
	dagnode* newnode = new dagnode(id, a);
	fprintf(stderr, "dagnode id: %d \n", newnode->getId());


	for (int i = 0; i < numberOfAncestors; i++){
		dagnode ancestor = a[i];
		if(a[i].getChilds() == 0){
			//No child exist earlier
			a[i].setChilds({newnode});
			fprintf(stderr, "Added first child\n");
		} else {
			dagnode child1 = a[i].getChilds()[0];
			dagnode newChildList[] = {child1,*newnode};
			a[i].setChilds(newChildList);
			fprintf(stderr, "Added second child\n");
		}
	}

	dagnodeMap[newnode->getId()] = *newnode;
}


At the second last line im trying to save a instance of my object to my map. But it gives an error:

/usr/include/c++/4.6/bits/stl_map.h: In member function ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = int, _Tp = dagnode, _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, dagnode> >, std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = dagnode, std::map<_Key, _Tp, _Compare, _Alloc>::key_type = int]’:
../DAG/dag.cpp:55:29: instantiated from here
/usr/include/c++/4.6/bits/stl_map.h:453:11: error: no matching function for call to ‘dagnode::dagnode()’
/usr/include/c++/4.6/bits/stl_map.h:453:11: note: candidates are:
../DAG/dagnode.h:16:2: note: dagnode::dagnode(int, dagnode*)
../DAG/dagnode.h:16:2: note: candidate expects 2 arguments, 0 provided
../DAG/dagnode.h:14:7: note: dagnode::dagnode(const dagnode&)
../DAG/dagnode.h:14:7: note: candidate expects 1 argument, 0 provided
make: *** [DAG/dag.o] Error 1


The object dagnode have an constructor, but i dont expect that should give any problem.

Are there any who can help me please.
Thanks a lot.
Does the object dagnode have a constructor that takes zero arguments?
No it does not. Should i have that?

The object (header file) looks like this:
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
/*
 * dagNode.h
 *
 *  Created on: Oct 10, 2012
 *      Author: theis
 */

#ifndef DAGNODE_H_
#define DAGNODE_H_
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

class dagnode {
public:
	dagnode(int idParam, dagnode *a);
	void setChilds(dagnode *c);
	dagnode* getChilds();
	int getId();
private:
	dagnode* ancestors[2]; //Containing 1 or 2 ancestors. Second is NULL if only one.
	dagnode* childs[2]; //Containing 1 or 2 children. Second is NULL if only one.
	int id;
};



#endif /* DAGNODE_H_ */ 


I don't want to make it possible to construct the object with out the parameters...
Hey. Have just tested it creating a constructer with no parameters. That works. But it is definitely not something i want. Can I avoid that?

But thanks a lot :)
Great help
Can I avoid that?


Yes. Make sure that none of your code attempts to create one without parameters.

Remove the constructor you just added, and then identify the lines that are trying to use it, and change them so they don't.
Yes but that is not just as safe. As good practice I would just make it fully impossible. I don't know if other users of my code understand the logic that im writting.
As good practice I would just make it fully impossible.


When you remove the constructor with no parameters (and have at least one other constructor), it IS impossible. Your code won't compile because the function does not exist. This is the problem your code had in your original post. You were trying to use a function that didn't exist, which is impossible.
Last edited on
if you want to use your class with std::map you need a constructor somehow without parameter.

you can achieve that by using default parameter:
1
2
3
4
5
class dagnode {
public:
	dagnode(int idParam = 0, dagnode *a = NULL); // or something more useful
...
};
Topic archived. No new replies allowed.