Thread: Class won't call

  1. #1
    Registered User Aalmaron's Avatar
    Join Date
    Jan 2004
    Location
    In front of a monitor
    Posts
    48

    Class won't call

    so this class that i wrote, won't allow me to call it, every time i try to make an instance of it, it tells me that the instance name need a semicolon before it.

    now i made a test class to see on a smaller scale what i was doing wrong, but my test class works perfectly, and i don't see any diference.

    there is my test class
    Code:
     	class cls
    	{
    	public:
    		void seti(string i);
    		string geti();
    	private:
    		string i;
    	};
    void cls::seti(string i)
    {
    	this -> i = i;
    }
    string cls::geti()
    {
    	return i;
    }
    and when i call it, i call it like this

    Code:
    cls a;
    	cout << "enter a word: ";
    	cin >> word;
    	a.seti(word);
    it works just fine.

    but in my actual program, that i need it to work in, i get the error. this is the class and its functions.

    Code:
     class info
    {
    	public:
    		info(char initname[101], float initcalories, float initcarbohydrates, float initfat, float initcholesterol, float initsodium, float initprotein);
    		~info();
    		void setname(char name[101]);
    		void setcalories(float calories);
    		void setcarbohydrates(float carbohydrates);
    		void setfat(float fat);
    		void setcholesterol(float cholesterol);
    		void setsodium(float sodium);
    		void setprotein(float protein);
    		char getname();
    		float getcalories();
    		float getcarbohydrates();
    		float getfat();
    		float getcholesterol();
    		float getsodium();
    		float getprotein();
    	private:
    		char name[101];		// name of the food
    		float calories;		// calories
    		float carbohydrates;// grams
    		float fat;			// grams
    		float cholesterol;	// grams
    		float sodium;		// grams
    		float protein;		// grams
    };
    
    void info::setname(char name[101])		{	this -> name[101] = name[101];	}
    void info::setcalories(float calories)		{	this -> calories = calories;	}
    void info::setcarbohydrates(float carbohydrates){	this -> carbohydrates = carbohydrates;	}
    void info::setfat(float fat)			{	this -> fat = fat;	}
    void info::setcholesterol(float cholesterol)	{	this -> cholesterol = cholesterol;	}
    void info::setsodium(float sodium)		{	this -> sodium = sodium;	}
    void info::setprotein(float protein)		{	this -> protein = protein;	}
    char info::getname()				{	return name[101];	}
    float info::getcalories()			{	return calories;	}
    float info::getcarbohydrates()			{	return carbohydrates;	}
    float info::getfat()				{	return fat;	}
    float info::getcholesterol()			{	return cholesterol;	}
    float info::getsodium()				{	return sodium;	} 
    float info::getprotein()			{	return protein;	}
    and when i call it, it looks like this

    Code:
     
    	static char tempName[101];
    	static float tempCalories;
    	static float tempCarbohydrates;
    	static float tempFat;
    	static float tempCholesterol;
    	static float tempSodium;
    	static float tempProtein;
    
    	cout << "name:\t\t\t> ";
    	cin.get(tempName[101]);
    	cin.ignore(100, '\n');
    
    	cout << "\ncalories:\t\t> ";
    	tempCalories = getfloat();
    
    	cout << "\ncarbohydrates:\t\t> ";
    	tempCarbohydrates = getfloat();
    
    	cout << "\nfat:\t\t\t> ";
    	tempFat = getfloat();
    
    	cout << "\ncholesterol:\t\t> ";
    	tempCholesterol = getfloat();
    
    	cout << "\nsodium:\t\t\t> ";
    	tempSodium = getfloat();
    
    	cout << "\nprotein:\t\t> ";
    	tempProtein = getfloat();
    	info alpha(tempName[101],tempCalories,tempCarbohydrates,tempFat,tempCholesterol,tempSodium,tempProtein);
    i know that i don't need all of those get and set functions, but they are there for a diferent part of hte code, and when i comment them out, it still won't work.

    these are hte 3 syntax errors that i'm getting
    error C2146: syntax error : missing ';' before identifier 'alpha'
    warning C4551: function call missing argument list
    error C3861: 'alpha': identifier not found, even with argument-dependent lookup
    i've gotta be missing something obvious. i really apreciate your help

    oh yeah, i'm using iostream, conio, iomanip, and ctype. and getfloat() only checks for good input, returns 0 if it is bad input, and makes it so that my program doesn't die when i have bad input.

    i've run it in both devC++ and visual studio.net

    edit: the functions taht let me manipulate the private variables in my class are formatted nicely in the IDE's, they just coppied over wierd, sorry if its harder to read. i'll try to fix it.
    Last edited by Aalmaron; 04-13-2006 at 04:14 PM.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Some simple issues:

    Code:
    this -> name[101] = name[101];
    strcpy(this->name, name);
    Code:
    return name[101];
    return name;
    Code:
    cin.get(tempName[101]);
    cin.get(tempName);
    Code:
    info alpha(tempName,tempCalories,tempCarbohydrates,tempFat,tempCholes  terol,tempSodium,tempProtein);
    Anyways, I feel like you simply did not include the header for your class in your main file.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I don't see anything in the code you posted that would cause those errors. You haven't defined the constructor/destructor for your info class, but that should cause a linker error.

    Is the class declaration in the same file as the last code snippet you posted? (I assume it isn't because it sounds like the compiler doesn't recognize info as a class.) If it's in a header file, make sure you've included that header in the file where you use the class.

    Edit: I'm slow
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Registered User Aalmaron's Avatar
    Join Date
    Jan 2004
    Location
    In front of a monitor
    Posts
    48
    Quote Originally Posted by JaWiB
    I don't see anything in the code you posted that would cause those errors. You haven't defined the constructor/destructor for your info class, but that should cause a linker error.

    Is the class declaration in the same file as the last code snippet you posted? (I assume it isn't because it sounds like the compiler doesn't recognize info as a class.) If it's in a header file, make sure you've included that header in the file where you use the class.

    Edit: I'm slow
    oh yeah, i forgot to include the constructor and destructor defenitions, they are there though. and yes, its all one big file. i'm not too familiar with making my own header files, so i thought that it might be that at first, and coppied it all over.

    the cass is declared before main, and the function that adds the data is after main. i don't see how any of this could be the error. if it helps i could post teh whole code, but i don't think that i'd help, and its 220 lines long atm. :-/

    any other leads?

    edit: i found a function that was declared, but not defined, or used, and i don't know how it got there, but it was called info. once i deleted that it worked just fine thanks for you help guys.
    Last edited by Aalmaron; 04-13-2006 at 06:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  2. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM