errors in composition
Jun 17, 2012 at 12:50pm UTC
hello,
i'm currently learning composition in c++. I created two classes which are tele and person, tele is a short name for telephone :P
tele class can store a person number.
tele.h
1 2 3 4 5 6 7 8 9 10
#ifndef TELE_H
#define TELE_H
class tele{
int Tnumber;
public :
tele(int n);
void printNumber();
};
#endif
tele.cpp
1 2 3 4 5 6 7 8 9 10 11
#include "tele.h"
#include <iostream>
using namespace std;
tele::tele(int n)
:Tnumber(n){}
void tele::printNumber()
{
cout << Tnumber << endl;
}
person.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#ifndef PERSON_H
#define PERSON_H
#include "tele.h"
#include <string>
class person{
std::string name;
tele t;
public :
person(std::string name, tele tt);
void printinfo();
};
#endif
person.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13
#include "tele.h"
#include "person.h"
#include <string>
#include <iostream>
using namespace std;
person::person(string name, tele tt)
: name(name), t(tt){}
void person::printinfo()
{
std::cout << "Name is: " << name << " and his telephone is: " << t.printNumber();
}
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13
#include "tele.h"
#include "person.h"
#include <string>
#include <iostream>
using namespace std;
int main(){
tele b(050);
person a("empror9" , b);
a.printinfo();
return 0;
}
when I compile I get so many errors. could you please tell me what is the problem?
thank you
Last edited on Jun 17, 2012 at 12:52pm UTC
Jun 17, 2012 at 12:55pm UTC
Shortening class names is a horrible idea, don't do it.
Also, class names start with an uppercase letter unless you're following the boost style (which you aren't) or a similar one.
could you please tell me what is the problem?
Maybe you should start with posting the errors?
Last edited on Jun 17, 2012 at 12:56pm UTC
Jun 17, 2012 at 12:58pm UTC
cout << " and his telephone is: " << t.printNumber();
t.printNumber() is a function that returns void - it returns nothing. You're trying to output a void object; how can you output something that doesn't exist?
Jun 17, 2012 at 1:15pm UTC
thanks Athar and Moschops
Moschops you are right! that was my error.
Athar thanks for addition information about naming a class.
Topic archived. No new replies allowed.