hi
im tryin to read some info (both characters and integers) from an input file and save them into the sorted list. it is a big project and i cant put all the files i have over here. so i'll just put the part im having a problem.
i have a class named NutritionFacts, which is this :

/*
typedef int Type1;


using namespace std;


#ifndef NUTRITIONFACTS_H
#define NUTRITIONFACTS_H


class NutritionFacts
{


public:
NutritionFacts();
NutritionFacts(char *name);
Type1 getNutritionFacts();
void setNutritionFacts(Type1 calories, Type1 fat, Type1 carbohydrates);
void display();
void read(ifstream & inFile);
private:
char _name [70];
Type1 _calories;
Type1 _fat;
Type1 _carbohydrates;


};


#endif
*/


and i have other classes of Node, List and SortedList. i set the data type into NutritionFacts like this : for example in the Node class


/*
#ifndef _NODE_H_
#define _NODE_H_


typedef NutritionFacts Type;


class Node {
public:
Node(Type x, Node *n = 0);
Type getInfo();
Node *getNext();
void setInfo(Type x);
void setNext(Node *n);
private:
Type info;
Node *next;
};


#endif
*/

its giving me the errors only on that "typedef NutritionFacts Type;" so far..
what am i doing wrong? thanks in advance.

I have 2 questios.

1: what do the error messages say?
2: It looks to me like the whole program is turned into a comment! If not, whats the /* doing?

>typedef NutritionFacts Type;
Header files don't magically know about each other. You need to tell them. For example, if node.h needs a name declared in nutritionfacts.h, you must include nutritionfacts.h in node.h before using the name.

Hey I have this question about header files.
Suppose i have myclass.h where the class structure is defined.
And then I have a myclass.cpp where methods are defined.
The myclass.cpp includes the myclass.h file.
Now say i need to use the myclass in another file.
Should I include myclass.h or myclass.cpp?
I usually do it by including myclass.cpp and it works.

But again i m not including header files(itz a .cpp file) and it makes me think if i m doing the right thing or atleast in the right way.

Member Avatar for Siersan

You should include myclass.h. Headers are there to provide declarations so that the code in multiple source files will compile. You do not need definitions until link time. You can include myclass.cpp, but the chances of it breaking something increase drastically because you are trying to include definitions. C++ has a rule that any name can only be defined once, so if you include myclass.cpp twice, the multiple definition will cause a linker error.

>> ...code in multiple source files will compile. You do not need definitions until link time.
Okay i have seen that i can compile the code even if i only include the .h file. But now it produces linker errors. What do i do to overcome this.

Member Avatar for Siersan

To compile the code you only need declarations. But to link the object files into an executable, you need the definitions. :) The conventional way of doing this is by having a .h file with declarations and a .cpp file with definitions. You include the .h file wherever you use any of the names and then compile/link the .cpp file with the rest of your project. The following example uses g++ as the compiler:

/* file.h */
#ifndef FILE_H
#define FILE_H

void function(); // Declaration

#endif
/* file.cpp */
#include "file.h"

void function()
{
  // Definition
}
/* main.cpp */
#include "file.h"

int main()
{
  function();
}
$ g++ main.cpp file.cpp
$ ./a.out

Notice how the header is only used for name resolution and isn't involved in the link stage. It's even more obvious if you do separate compilation and link steps:

$ g++ -c main.cpp file.cpp
$ g++ main.o file.o
$ ./a.out

Hmm I think I had seen something like that when i was learning C from "A Book on C". But i did not have access to Unix computers then. Now in my College they do have unix, but in the lab our professor taught us to compile things including the .cpp files. So i guess i never get to learn the right thing. Is there any way I could do the samething in my home pc like separate compilation and linking. I have borland and DevC++. The only way i know to compile codes using these compilers is to hit the crtl+F9(compile) or something similar with the file containing my main() function open and active. And when i dont include the .cpp file it ends up producing linker errors. What shud i do?

Member Avatar for Siersan

As long as the structure is correct for your project, you shouldn't get any errors. The nice thing about IDEs is that they package all of your tools together, but they also let you structure multiple files so that everything works when linked together. In a command line environment, we have to rely on make files to handle the same dependencies.

Can you shrink your project down into just the declarations and definitions, then post the individual files like I did? Then I can (hopefully) tell you where the problem is. :)

The thing is that i dont create projects to do my work.

I use an external editor and write my codes there. I just complie the file that contains the main function. That's what i do with both DevC++ and borland 5.5. If i include the dateandtime.h file instead of the dateandtime.cpp file here in my driver.cpp file i get linker errors:

in file dateandtime.h

#include<ctime>
#include<iostream>

using namespace std;

class MyTime
{
	tm *t;
	
	public:
	MyTime();
	char * GetTime();
	char * SetDay(int d);
	char * SetMonth(int m);
	char * SetYear(int y);
	char * AddDays(int d);
	void Difference();
	

	
	
};

in file dateandtime.cpp

#include"dateandtime.h"

MyTime::MyTime()
{
	time_t temp;
	time(&temp);
	t = localtime(&temp);
	
}

char * MyTime::GetTime()
{
	return asctime(t);
}

char * MyTime::SetDay(int d)
{
	t->tm_mday = d;
	time_t temp;
	temp = mktime(t);
	t = localtime(&temp);
	return asctime(t);
	
}

char * MyTime::SetMonth(int m)
{
	t->tm_mon = m - 1;
	time_t temp;
	temp = mktime(t);
	t = localtime(&temp);
	return asctime(t);
}


char * MyTime::SetYear(int y)
{
	t->tm_year = y % 1900;
	time_t temp;
	temp = mktime(t);
	t = localtime(&temp);
	return asctime(t);
}
	
char * MyTime::AddDays(int d)
{
	t->tm_mday += d;
	time_t temp;
	temp = mktime(t);
	t = localtime(&temp);
	return asctime(t);
	
}

void MyTime::Difference()
{
	time_t time1;
	time_t time2;
	time1 = mktime(t);
	tm temp;
	cout<<"\nCurrent Date and Time: "<<ctime(&time1);
	cout<<"Enter New Date(dd/mm/year): ";
	int value;
	
	cin>>value; 
	temp.tm_mday = value;
	
	
	cin>>value; 
	temp.tm_mon = value - 1;
	
	
	cin>>value; 
	temp.tm_year = value % 1900;
		
	cout<<"Enter New Time(hour: min: sec) 24 hours Format: ";
	cin>>value; 
	temp.tm_hour = value + 1;
	
	cin>>value; 
	temp.tm_min = value;
	
	cin>>value; 
	temp.tm_sec = value;
	
	time2 = mktime(&temp);
	cout<<"\nSubstracting Date and time: "<<ctime(&time2);
	cout<<"Difference in Seconds: "<<difftime(time1, time2);
	
}

in file driver.cpp

#include<iostream>
#include<conio.h>
#include<cstdio>


#include"dateandtime.cpp"//works now but doesnt work if i only include dateandtime.h instead

void isleapyear();
void add_seconds(int sec);

int main()
{
	MyTime atime;
	
	int choice;
	for(;;)
	{
		
		cout<<"Current Date and Time: "<<atime.GetTime()<<endl;
		cout<<"\n1.Set Day";
		cout<<"\n2.Set Month";
		cout<<"\n3.Set Year";
		cout<<"\n4.Add Day(s) to current time";
		cout<<"\n5.Add Seconds to time since 1970";
		cout<<"\n6.Find Difference";
		cout<<"\n7.Check leap-year";
		cout<<"\n\n8.Exit\n\n";
		
	
		
		
	
		cin>>choice;
		
		switch(choice)
		{
			case 1:
				int day;	
				do{
					cout<<"\nEnter New Day: ";
					cin>>day;
				}while(day < 1 || day > 31);
				cout<<"\nChanged date and time: "<<atime.SetDay(day);
				getch();
				break;
				
				
			case 2:
				int month;	
				do{
					cout<<"\nEnter New Month: ";
					cin>>month;
				}while(month < 1 || month > 12);
				cout<<"\nChanged date and time: "<<atime.SetMonth(month);
				getch();
				break;
				
			case 3:
				cout<<"\nEnter New Year: ";
				int year;
				cin>>year;
				cout<<"\nChanged date and time: "<<atime.SetYear(year);
				getch();
				break;
				
			case 4:
				cout<<"\nEnter no of Days to add: ";
				int days;
				cin>>days;
				cout<<"\nChanged date and time: "<<atime.AddDays(days);
				getch();
				break;
				
			case 5:
				cout<<"\nEnter seconds to add: ";
				int sec;
				cin>>sec;
				add_seconds(sec);
				getch();
				break;
				
			case 6:
				atime.Difference();
				getch();
				break;
			
			case 7:
				isleapyear();
				getch();
				break;
				
			case 8:
				exit(0);				
				
		}
	}
		
	
	
}


void add_seconds(int sec)
{
	time_t t;
	time(&t);
	cout<<"\nSeconds since January 1, 1970: "<<t;
	cout<<"\nDate and Time: "<<ctime(&t);
	t += sec;
	cout<<"\nSeconds since January 1, 1970 + "<<sec<<" = "<<t;
	cout<<"\nChanged Date and Time: "<<ctime(&t);
	
}

void isleapyear()
{
	cout<<"\nEnter a new year: ";
	int year;
	cin>>year;
	if ( (year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
		cout<<endl<<year<<" is a leap year";
	else
		cout<<endl<<year<<" is not a leap year";
	cout<<endl;
}

The errors are(when i only include dateandtime.h) identical in both borland and DevC++, both saying that they cannot find references to the methods defined in dateandtime.cpp.

The thing is that i dont create projects to do my work.

You may want to investigate makefiles. Then you can use your favorite editor and still write modular code the correct way.

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.