In my code my "ndeps" is not being declared in my "setdepends" function and i dont know why. Can anyone help me figure out why?

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

class Object{

private:
	Object **depends;
	int ndepends;

public:
	Object();
	char *id; 
	enum Color { BLUE, BROWN, YELLOW };
	Color color;
	
		 
	Object (char *id){
		this->id = new char[strlen(id)+1];
		strcpy(this->id, id);
		color = BLUE;
	}
	void setdeps(Object **deps, int ndeps){
		ndepends = ndeps;
		depends = deps;
	}
	~Object () {
		delete id;
		delete depends;
	}
	
	void topo(){
		if (color == YELLOW) 
			return;
		if (color == BROWN){
			cout << "uh oh\n";
			exit(0);
		}
		color = BROWN;
		for(int i=0; i<ndepends; i++) 
			depends[i]->topo();
		cout << "  " << id;
		color = YELLOW;
	}
	void Show(){
		cout << id << " : ";
		for( int i=0; i < ndepends; i++) cout << depends[i]->id
			<< " ";
		cout << "\n";
	}
	
	
	void init (char *);

};
class Graph{

private:

	Object *objects;
	int nobjects;

public:

	fstream fin;
	char buffer[1024];

	Graph (char *filename){
		fin.open(filename, ios::in);
		if (fin.fail()){
			cerr << "Cannot Open " << filename << "\n";
			exit (0);
		}
	}
	
	~Graph () {
		delete objects;
	}

	void getObjects () {
		for ( nobjects = 0; fin >> buffer ; nobjects++ )
			if (buffer == "-") break;
		objects = new Object[nobjects];
		fin.seekg(0 , ios::beg);
		for (int i=0; fin >> buffer && buffer[0] != '-'; i++)
			objects[i].init(buffer);

	}

	void setDepends (int i) {
		size_t mark = fin.tellg();
		for ( ndeps=0; fin >> buffer && buffer[0] != '-'; ndeps++);
		Object **deps = new Object*[ndeps];
		fin.seekg(mark, ios::beg);
		for ( ndeps=0; fin >> buffer && buffer[0] != '-'; ){
			int n=atoi(buffer);
			if (0<=n && n<nobjects) deps[ndeps++]=&objects[n];
		}
		objects[i].setdeps(deps, ndeps);
	}

	void setDepend (){
		for (int i=0; i < nobjects; i++)
			setDepends(i);
	}

	void Sort() {
		for (int i=0; i < nobjects; i++)
			objects[i].topo();
		cout << "\n";
	}

	void Show(){ for (int i=0; i<nobjects; i++) objects[i].Show();
	}


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

Recommended Answers

All 7 Replies

Your variable ndeps is nowhere to be found within your code you must first declare it

Since you're using it in both classes and they are not related I would suggest declaring a global variable.

Alright thanks. i put a global variable of int ndeps; but now on line 85 i am getting an error saying Object' : no appropriate default constructor available. I tried writing an Object(); in my object class but that didnt help. what can i do?

Look at line 94: for ( ndeps=0; fin >> buffer && buffer[0] != '-'; ndeps++); There's one thing that's missing there and one thing that's extra. You need to declare ndeps as an int (since the other ndeps in the program is unrelated making the global variable is definitely not a good idea) either within the loop body or before the first for loop -- beforehand for sure if you want to use it for both loops in that method.

More importantly, this for loop won't even execute because of the extra semicolon.

There are some scoping issues (with deps) that come up after you resolve all this but see if you can figure it out.

Alright thanks. i put a global variable of int ndeps;

I would avoid global thing and instead I would declare a variable and initialize it in each class' constructor

class Object{
public:
    Object(){
        this->ndeps = 5;
    }
private:
    int ndeps;
};

class Graph{
public:
    Graph(){
        this->ndeps = 5;
    }
private:
    int ndeps;
};

The OP is only using it as a loop variable in Graph. I don't think there's a need to declare it all over the place. In object it's just a "dummy" parameter variable so it's not necessary there either.

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

class Object{

private:
	Object **depends;
	int ndepends;
	

public:
	char *id; 
	enum Color { BLUE, BROWN, YELLOW };
	Color color;
	
	Object (char *id){
		this->id = new char[strlen(id)+1];
		strcpy(this->id, id);
		color = BLUE;
		
	}
	void setdeps(Object **deps, int ndeps){
		ndepends = ndeps;
		depends = deps;
	}
	~Object () {
		delete id;
		delete depends;
	}
	
	void topo(){
		if (color == YELLOW) 
			return;
		if (color == BROWN){
			cout << "uh oh\n";
			exit(0);
		}
		color = BROWN;
		for(int i=0; i<ndepends; i++) 
			depends[i]->topo();
		cout << "  " << id;
		color = YELLOW;
	}
	void Show(){
		cout << id << " : ";
		for( int i=0; i < ndepends; i++) cout << depends[i]->id
			<< " ";
		cout << "\n";
	}
	
	void init (char *);

};
class Graph{

private:

	Object *objects;
	int nobjects;
	
public:

	fstream fin;
	char buffer[1024];


	Graph (char *filename){
		fin.open(filename, ios::in);
		if (fin.fail()){
			cerr << "Cannot Open " << filename << "\n";
			exit (0);
		}
	}	
	
	~Graph () {
		delete objects;
	}

	void getObjects () {
		for ( nobjects = 0; fin >> buffer ; nobjects++ )
			if (buffer == "-") break;
		objects = new Object[nobjects];
		fin.seekg(0 , ios::beg);
		for (int i=0; fin >> buffer && buffer[0] != '-'; i++)
			objects[i].init(buffer);

	}
	void setDepends (int i) {
		int ndeps;
		size_t mark = fin.tellg();
		for ( ndeps=0; fin >> buffer && buffer[0] != '-'; ndeps++)
		Object **deps = new Object*[ndeps];
		fin.seekg(mark, ios::beg);
		for ( ndeps=0; fin >> buffer && buffer[0] != '-'; ){
			int n=atoi(buffer);
			if (0<=n && n<nobjects) deps[ndeps++]=&objects[n];
		}
		objects[i].setdeps(deps, ndeps);
	}

	void setDepend (){
		for (int i=0; i < nobjects; i++)
			setDepends(i);
	}

	void Sort() {
		for (int i=0; i < nobjects; i++)
			objects[i].topo();
		cout << "\n";
	}

	void Show(){ for (int i=0; i<nobjects; i++) objects[i].Show();
	}
};
int main(int argc, char **argv)
{	
	if (argc != 2) {  
      cout << "\nUsage: " << argv[0] << " <filename>\n";
      exit (0);
	}
	Graph *graph = new Graph(argv[1]);
	graph->getObjects();
	graph->setDepend();
	graph->Sort();
	graph->Show();
}

Im still having trouble as to why in my getobjects function objects = new Object[nobjects]; is not finding the constructor?

No where in constructor you are initializing nobjects
initialize it somewhere in constructor

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.