I've been searching for the cause for most of two days now. I'm not a beginning programer, but I am new to C++. suspect I am missing two things or just the concept.
**Error messages **
1> mainperson.cpp
1>c:\prog\upload\mainperson.cpp(19): error C2660: 'personType::getlastname' : function does not take 1 arguments
1>c:\prog\upload\mainperson.cpp(20): error C2660: 'personType::getfirstname' : function does not take 1 arguments
1>c:\prog\upload\mainperson.cpp(21): error C2352: 'personType::print' : illegal call of non-static member function
1> c:\prog\upload\persontype.h(10) : see declaration of 'personType::print'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
// ff 11/11/2010 personType.h
#include <iostream>
#include <string>
#include <cstdio>
usingnamespace std;
class personType
{
public:
void print() const; // outputs first and last name
void setname(string first, string last); //function to set fname an lname to paramters after firstname = first and lastname = last
std::string getfirstname() const; // function to return the first name after the value of firrst name is returned
std::string getlastname() const; // function to return the last name after the value of last name is returned
personType(); // default constructor sets first and last name to null strings
personType(string first, string last); // constructor with parameters
// destructor
~personType();
private:
string firstname; //variable to store firstname
string lastname; // varaible to store lastname
}
;
void personType::print() const
{
cout << firstname << " " << lastname;
}
void personType::setname(string first, string last)
{
firstname = first;
lastname = last;
}
std::string personType::getfirstname() const
{
return firstname;
}
std::string personType::getlastname() const
{
return lastname;
}
personType::personType() //default constructor
{
firstname = "";
lastname = "";
}
personType::personType(string first, string last) //constructor with parameters.
{
firstname = first;
lastname = last;
}
;
******************************* main follows
#include <iostream>
#include <cstdio>
#include <string>
#include "personType.h"
usingnamespace std;
int main()
{
string fname = "";
string lname = "";
cout << "enter a first name ";
cin >> fname;
cout << "enter a last name ";
cin >> lname;
personType::personType(fname, lname);
personType names(fname, lname) ;
personType::getlastname(lname);
personType::getfirstname(fname);
personType::print();
// personType::print();
return (0);
}
Yes, it's the concept. You need to write it like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int main()
{
string fname = "";
string lname = "";
cout << "enter a first name ";
cin >> fname;
cout << "enter a last name ";
cin >> lname;
personType names(fname, lname) ;
names.getlastname(lname);
names.getfirstname(fname);
names.print();
return (0);
}
With '::' the functions are static (you have to write that key word in front of the function within the class) and only connected to the class. With '.' the functions are connected to the object (here: names)