I am writing a sort program and i am getting the error " error: no matching function for call to 'Object::setdepend(Object*&)' " in this line " graph->setdepend(graph[atoi(buffer.c_str())]); " which is in my read function. I am not sure how to fix this, can anyone help?


#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;

class Object{
private:
	Object **depends;
	std::string ident;
	int status;
	int current;
	int ndepends;

public:
	Object(std::string id){
		status=0;
		ident=id;
		depends=NULL;
		ndepends=0;
		current=0;
	}

	~Object() { delete depends; }

	void setdepends(int n){
		ndepends=n;
		depends = new Object*[n];
	}

	void setdepend(Object **graph, int idx){
		if (current >= ndepends) return;
		depends[current++] = graph[idx];
	}

	void topo(){
		if (status==2) return;
		if (status==1){
			cout << "uh oh\n";
			exit(0);
		}
		status=1;
		for (int i=0; i < ndepends; i++) depends[i]->topo();
		cout << ident << " ";
		status=2;
	}
	void Show(){
		cout << ident << " : ";
		for( int i=0; i < ndepends; i++) cout << depends[i]->ident
			<< " ";
		cout << "\n";
	}
	
};
class Graph{
	Object **graph;
	int nobjects;

public:
	Graph(){ nobjects=0; graph=NULL; }

	~Graph(){
		for(int i=0; i<nobjects; i++) delete graph[i];
		delete graph;
	}

	void read(char *filename){
		fstream fin;
		fin.open(filename, ios::in);
		std::string buffer;
		nobjects=0;
		fin >> buffer;
		while(buffer != "-"){
			nobjects++;
			fin>>buffer;
		}
		fin.seekg(0,ios::beg);

		graph = new Object*[nobjects];
		fin >> buffer;
		int i=0;
		while (buffer != "-"){
			graph[i++] = new Object(buffer.c_str());
		}
		for (int i=0; i<nobjects; i++){
			long mark = fin.tellg();
			int ndepends=0;
			fin.seekg(0,ios::beg);
			graph[i]->setdepends(ndepends);
		}
		fin >> buffer;
		while(buffer != "-"){
			graph[i]->setdepend(graph[atoi(buffer.c_str())]);
			fin>>buffer;
		}
	}
	
	void show() { 
		for (int i=0; i < nobjects; i++ ) graph[i]->Show();
	}

	void topo() {
		for (int i=0; i < nobjects; i++ ) graph[i]->topo();
	}

};

int main(int argc, char **argv)
{	
	if (argc != 2) {  
      cout << "\nUsage: " << argv[0] << " <filename>\n";
      exit (0);
	}
	Graph *graph = new Graph();
	graph->show();
	graph->topo();
	graph->read(argv[1]);
}

Well, I'm not sure about how all the stuff you have crammed into that line works, but I do know that you are trying to call setdepend() with only 1 argument and it requires 2. What did you forget to send to it?

[edit]
wait... nvm.... You have 2 functions with confusingly similar names, I was looking at the wrong one...

[edit2]
hmmm...... No, I was looking at the correct one. You are calling setdepend(), but your call is set up more like a call to setdepends().
Do you see what I mean about the confusingly similar names?

Yes well i could change the names i suppose. However this line " graph->setdepend(graph[atoi(buffer.c_str())]); " was given to me by my professor, so i didnt forget anything in it.

Just because he gave it to you, doesn't mean it's correct. I can't begin to tell you about all the compile errors I had to fix in instructor-issued files this past semester. There was always something that had to be fixed before it would work.

All that it being instructor-issued means is that either your professor forgot something, or your implementation of the Object::setdepend() method is incorrect.

Either way, the call does not match the definition and something has to change. You need to either:
1. add a default value to the idx parameter (probably not a good choice for this situation)
2. eliminate the idx parameter
3. correct the format of your call

I suggest you take a closer look at the writeup for your assignment to see what the proper requirements are.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.