following is my main program
Linklist is a class of circular Linked list
so my question is since the parameter of function doesn't contain Linklist in it
can I go ahead and call it "List" in any function, such as List.print(), anyway?

main()
{ linklist list;
  int choice, num;
  do
  { system("cls");
    choice=menu();
	switch(choice)
	{ case 1: cout<<"Enter integer to insert:";
			  cin>>num;
			  list.insert(num); break;
	  case 2: list.print(); break;
	  case 3: cout<<"How many random integers?";
			  cin>>num;
			  list.fill(num); break;
	  case 4: { linklist copy(list);
				cout<<"copy:";
				copy.print();
			  } break;
	  case 5: cout<<"Enter the number to delete:";
			  cin>>num;
			  list.deletenode(num); break;
	  case 6: list.destroy(); break;
	  case 7: checkparameter(list); break;
	  case 9: break;
	  default: cout<<"Invalid choice\n";
			   cin.ignore(); cin.ignore();
	}
  } while(choice!=9);
}

Yes and no. It depends on where you declare the 'list' (not 'List') variable. If the variable is visible in the scope, you can call it in the function; otherwise, you will get an error when you compile your code. You should read about variable scope.

/////////////////
// 1st example
int main() {
  linklist list;
  ...
}

void printList() {
  list.print();  // <-- get an error because list is not visible to this scope
}

////////////////
// 2nd example
// (I don't know how you implement linklist, so this example may not call linklist
//  function properly)
int main() {
  linklist list;
  ..
  printList(list);
}

printList(linklist l) {
  l.printList();  // <--  would be OK because you have passed the variable in here
}

////////////////
// 3rd example
int main() {
  linklist list;
  ..
  if (iWantToPrint) {
    list.printList();  // <-- this is OK because list is visible in this scope
  }
}

Yes and no. It depends on where you declare the 'list' (not 'List') variable. If the variable is visible in the scope, you can call it in the function; otherwise, you will get an error when you compile your code. You should read about variable scope.

/////////////////
// 1st example
int main() {
  linklist list;
  ...
}

void printList() {
  list.print();  // <-- get an error because list is not visible to this scope
}

////////////////
// 2nd example
// (I don't know how you implement linklist, so this example may not call linklist
//  function properly)
int main() {
  linklist list;
  ..
  printList(list);
}

printList(linklist l) {
  l.printList();  // <--  would be OK because you have passed the variable in here
}

////////////////
// 3rd example
int main() {
  linklist list;
  ..
  if (iWantToPrint) {
    list.printList();  // <-- this is OK because list is visible in this scope
  }
}

hmmm... that kind of answers my question
but I'm still confused, maybe because I didn't put the question correctly
I'm using visual studio 2010 and there are 3 files named
main.cpp, linklist.h, linklist.cpp
the definition of the class is in the linklist.cpp
so by looking at the function in the main(it is not changeable)
how to I call the list, such as in list.print() with no parameter

I think you cannot call a variable that is defined in other file atleast. This will be out of scope error or variable not defined message you can expect. So check out for scope before using it. Even if you place the main in .cpp file you cannot use it that way

forgot to mention. but you can do it otherway. declare it in class and you can use it in main.

If your main.cpp has include header for linklist.h, then you can create a list object inside the main.cpp. Now, you can call print for the list.

#include linklist.h

int main() {
  linklist list;

  // create and add whatever to list
  // then you can print() if it is the 'linklist' member function
}
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.