CustomerInfo.cpp
#include "CustomerInfo.h" // <==== Funtion Definition
CustomerInfo::CustomerInfo() // <==== The Scope Resolution which is two colons :: gives me access to the class
{
newZipCode = 0;
}
CustomerInfo::CustomerInfo(string name, string address, int zipCode)
{
newName = name;
newAddress = address;
newZipCode = zipCode;
}
CustomerInfo::~CustomerInfo()
{
}
string CustomerInfo::getName() const
{
return newName;
}
string CustomerInfo::getAddress() const
{
return newAddress;
}
int CustomerInfo::getZipCode() const
{
return newZipCode;
}
The main.cpp file
#include <iostream>
#include <string>
#include "CustomerInfo.h"
using namespace std;
int main()
{
string name;
string address;
int zipCode;
cout << "Enter your name: ";
getline (cin, name);
cout << "Enter your address" << endl;
getline (cin, address);
cout << "Enter your ZipCode: ";
cin >> zipCode;
CustomerInfo Real_1(name, address, zipCode);
cout << endl << " Name: " << Real_1.getName() << endl <<
"Address: " << Real_1.getAddress() << endl <<
"ZipCode: " << Real_1.getZipCode() << endl;
return 0;
}
The CustomerInfo.h file
#ifndef CUSTOMERINFO_H
#define CUSTOMERINFO_H
// Header ==> Function Declaration
#include <iostream>
#include <string>
using namespace std;
class CustomerInfo
{
public:
CustomerInfo(); // <==== Default Constructor
CustomerInfo(string, int); // <==== Overload Constructor
~CustomerInfo(); // <===== Destructor - Done using an object it will be destroyed out of memory'
string getName() const; // <==== Accessor Functions - Return member variables one value at a time. In addition, no void function will be used.
// getName - returns name of person
string getAddress() const;
// getAddress - returns address of person
int getZipCode() const;
// getZipCode - returns zipcode of person
private:
//Member Variables
string newName;
string newAddress;
int newZipCode;
}; // <=== Requires semicolon after brackets for classes
#endif // CUSTOMERINFO_H
The error I receive is out-of-line definition of 'CustomerInfo' does not match any declaration in 'CustomerInfo'
The following line is the error
CustomerInfo::CustomerInfo(string name, string address, int zipCode)
Your problem seems to be that you define a constructor
CustomerInfo::CustomerInfo(string name, string address, int zipCode)
which does not appear in the class definition (in CustomerInfo.h). The closest one is
CustomerInfo(string, int);
but it doesn't match the signature. Just replace it with
CustomerInfo(string name, string address, int zipCode);
to your class definition in the header file.
Related
main.cpp
#include "Artist.h"
#include "Artwork.h"
#include <iostream>
#include <string>
using namespace std;
int main() {
string userTitle, userArtistName;
int yearCreated, userBirthYear, userDeathYear;
getline(cin, userArtistName);
getline(cin, userTitle);
cin >> userBirthYear;
cin >> userDeathYear;
cin >> yearCreated;
Artist userArtist = Artist(userArtistName, userBirthYear, userDeathYear);
Artwork newArtwork = Artwork(userTitle, yearCreated, userArtist);
newArtwork.PrintInfo();
}
Artwork.cpp
#include "Artwork.h"
#include <iostream>
// TODO: Define default constructor
Artwork::Artwork(){
title = "Unknown";
yearCreated = -1;
}
// TODO: Define second constructor to initialize
// private fields (title, yearCreated, artist)
Artwork::Artwork(string title, int yearCreated, Artist artist){
this->title = title;
this->yearCreated = yearCreated;
this->artist = artist;
}
// TODO: Define get functions: GetTitle(), GetYearCreated()
string Artwork::GetTitle(){
return title;
}
int Artwork::GetYearCreated(){
return yearCreated;
}
// TODO: Define PrintInfo() function
// Call the PrintInfo() function in the Artist class to print an artist's information
void Artwork::PrintInfo(){
cout << "Title: " << title << ", " << yearCreated<< endl;
}
Artwork.h
#ifndef ARTWORKH
#define ARTWORKH
#include "Artist.h"
class Artwork{
public:
Artwork();
Artwork(string title, int yearCreated, Artist artist);
string GetTitle();
int GetYearCreated();
void PrintInfo();
private:
// TODO: Declare private data members - title, yearCreated
string title;
int yearCreated;
// TODO: Declare private data member artist of type Artist
Artist artist;
};
#endif
Artist.h
#ifndef ARTISTH
#define ARTISTH
#include <string>
using namespace std;
class Artist{
public:
Artist();
Artist(string artistName, int birthYear, int deathYear);
string GetName() const;
int GetBirthYear() const;
int GetDeathYear() const;
void PrintInfo() const;
private:
// TODO: Declare private data members - artistName, birthYear, deathYear
string artistName;
int birthYear;
int deathYear;
};
#endif
Artist.cpp
#include "Artist.h"
#include <iostream>
#include <string>
using namespace std;
// TODO: Define default constructor
Artist::Artist(){
artistName = "Unknown";
birthYear = -1;
deathYear = -1;
}
// TODO: Define second constructor to initialize
//private fields (artistName, birthYear, deathYear)
Artist::Artist(string artistName, int birthYear, int deathYear){
this->artistName = artistName;
this->birthYear = birthYear;
this->deathYear = deathYear;
}
// TODO: Define get functions: GetName(), GetBirthYear(), GetDeathYear()
string Artist::GetName() const{
return artistName;
}
int Artist::GetBirthYear()const {
return birthYear;
}
int Artist::GetDeathYear() const{
return deathYear;
}
// TODO: Define PrintInfo() function
void Artist::PrintInfo() const{
if( birthYear >=0 && deathYear == -1){
cout << "Artist: " << artistName << "(" << birthYear << "to present"<< ")"<< endl;
}
else if(birthYear >=0 && deathYear >=0){
cout << "Artist: " << artistName << "(" << birthYear << "to " << deathYear << ")"<< endl;
}
else{
cout << "Artist: " << artistName << " (" << "unknown" << ")"<< endl;
}
}
Given main(), complete the Artist class (in files Artist.h and Artist.cpp) with constructors to initialize an artist's information, get member functions, and a PrintInfo() member function. The default constructor should initialize the artist's name to "unknown" and the years of birth and death to -1. PrintInfo() displays "Artist:", then a space, then the artist's name, then another space, then the birth and death dates in one of three formats:
(XXXX to YYYY) if both the birth and death years are nonnegative
(XXXX to present) if the birth year is nonnegative and the death year is negative
(unknown) otherwise
Complete the Artwork class (in files Artwork.h and Artwork.cpp) with constructors to initialize an artwork's information, get member functions, and a PrintInfo() member function. The default constructor should initialize the title to "unknown", the year created to -1. PrintInfo() displays an artist's information by calling the PrintInfo() function in the Artist class, followed by the artwork's title and the year created. Declare a private field of type Artist in the Artwork class.
Ex: If the input is:
Pablo Picasso
Three Musicians
1881
1973
1921
1881 and 1973 being the birth and death years respectively, with 1921 being the year the work was created, the output is:
Artist: Pablo Picasso (1881 to 1973)
Title: Three Musicians, 1921
My issue Is when I test this input I only get... (in a different .h file)
Title: Distant Muses, 2000
I was wondering why void Artist::PrintInfo() wont output anything and would be great if anyone could guide me to the right direction!
Thank You in advance!
Reading through description, your Artwork class should contain PrintInfo() method, which on it's own should call Artist::PrintInfo() once invoked. Add printing the artist info before printing it's own info in the Artwork::PrintInfo() method and you should be good to go. It should look like this:
void Artwork::PrintInfo(){
artist.Printinfo(); // Not 100% sure this is correct syntax, check carefully.
cout << "Title: " << title << ", " << yearCreated<< endl;
}
Also, worth noting that you can 'merge' constructors. Instead of this(Yes I'm aware it's 'required', but you can take a step further):
Artist::Artist(){
artistName = "Unknown";
birthYear = -1;
deathYear = -1;
}
// TODO: Define second constructor to initialize
//private fields (artistName, birthYear, deathYear)
Artist::Artist(string artistName, int birthYear, int deathYear){
this->artistName = artistName;
this->birthYear = birthYear;
this->deathYear = deathYear;
}
You can have this:
Artist::Artist(string artistName="Unknown", int birthYear=-1, int deathYear=-1){
this->artistName = artistName;
this->birthYear = birthYear;
this->deathYear = deathYear;
}
By setting default values, if constructor isn't provided any value, aka default constructor is used, everything will be set to default value, in this case "Unknown" and -1's. If someone provides name, you will have name and -1's and so on.
I have this requirement.
I am trying to make a simple database schema, a little different than what I have seen in here. I have a class file (client.h) with it's implementation (client.cpp):
#ifndef CLIENT_H_
#define CLIENT_H_
#include <iostream>
using namespace std;
class Client {
public:
// constructors
Client();
Client(string new_name, string new_tel, string new_addr);
// getters
string getName();
string getAddr();
string getTel();
// setters
void setName(string);
void setAddr(string);
void setTel(string);
void display();
void input();
private:
// fields
string name;
string addr;
string tel;
};
#endif /* CLIENT_H_ */
/*
*ad client.cpp
*
* Created on: Jan 12, 2017
* Author: niksarid
*/
#include <iostream>
#include "client.h"
using namespace std;
Client::Client() {
setName("");
setAddr("");
setTel("");
}
Client::Client(std::string new_name, std::string new_addr, std::string new_tel) {
setName(new_name);
setAddr(new_addr);
setTel(new_tel);
}
string Client::getName() {
return name;
}
string Client::getAddr() {
return addr;
}
string Client::getTel() {
return tel;
}
void Client::setName(string p_name) {
name = p_name;
}
void Client::setAddr(string p_addr) {
addr = p_addr;
}
void Client::setTel(string p_tel) {
tel = p_tel;
}
void Client::input() {
string tmp;
cout << "INPUT CLIENT INFO" << endl;
cout << "Name: ";
cin >> tmp;
setName(tmp);
cout << "Address: ";
cin >> tmp;
setAddr(tmp);
cout << "Telephone: ";
cin >> tmp;
setTel(tmp);
}
void Client::display() {
cout << name << "\t" << addr << "\t" << tel << endl;
}
So I am trying to make a Company class that will hold a vector of Clients and at the startup of the program it will load the datafile "clients.dat", into the vector. I will be able to add a client or delete a client from the vector. At the end the vector will be saved back to "clients.dat".
So, the (company.h) file is like this:
class Company {
public:
Company();
~Company();
void add_client();
void print_clients();
void loadClientsFromFile();
void saveClientsToFile();
private:
vector<Client> clients;
} cmp;
#endif /* COMPANY_H_ */
but I can't seem to reference clients vector in any of the public methods of the class company.
EDIT: Sorry! Forgot the important part!!
For example when I try to add_client(),
void add_client() {
Client c;
c.input();
clients.push_back(c);
}
but I get
../src/company.cpp:49:2: error: 'clients' was not declared in this scope
clients.push_back(c);
So, how to achieve that?
As Morgan mentioned in the comments, this problem typically arises when you try to define the member function in your implementation file, but forget to add the class name as prefix (e.g. void add_client() {} instead of void Company::add_client() {}.
This mistake is common and can easily go unnoticed, since it is perfectly legal to define a new free function called add_client in your file, that would have nothing to do with the Company class. That's why the compiler only complains when you try to access a data member, but not before.
I am writing a program where I need to use copy constructor. Since I am novice in using copy constructor I do not know whether my declaration and using of copy constructor is valid or not?
Also here I am facing problem with the display function, the error is ::
error: prototype for 'int Student::display_student()' does not match any in class 'Student'. What is this error?
#include <string>
#include <iostream>
using namespace std;
class Student
{
private:
int rollno;
string name;
public:
Student();
Student(int x, string str);
Student(Student &s);
void display_student();
};
Student::Student()
{
rollno = 0 ;
name = "" ;
}
Student::Student(int x, string str)
{
rollno=x ;
name=str ;
}
Student::Student(Student &s)
{
rollno = s.rollno ;
name = s.name;
}
Student::display_student()
{
cout << "Student Name ::" << name << endl << "Student Roll No. ::" << rollno << endl;
}
int main()
{
Student A;
Student B(09,"Jhonny");
Student C(B);
A.display_student();
B.display_student();
C.display_student();
return 0;
}
You didn't specify the return value in the definition of Student::display_student(). Try:
void Student::display_student()
{
cout << "Student Name ::" << name << endl << "Student Roll No. ::" << rollno << endl;
}
Compiler assumes int Student::display_student() by default. The class declaration contains the prototype for void display_student() but you provided only the definition for function int display_student().
Copy constructor signature uses generally const reference. In your case, you may use the default implementation:
Student(const Student&) = default;
You may also add copy assignment:
Student& operator=(const Student&) = default;
Good afternoon!
Sorry if the question seems rather vague, but here's some (incomplete) code for some context. Specifically, this is about the "UserInfo inputInfo" definition part as seen in the functions UserInfo::setUserInfo() and UserInfo::displayProfile() in the implementation file.
project02.cpp (implementation file)
#include <iostream>
#include "project02.h"
using namespace std;
void UserInfo::setUserInfo()
{
UserInfo inputInfo;
string fName;
string lName;
int bYear;
string city;
string occupation;
cout << "Please enter your first name: ";
cin >> fName;
inputInfo.setFirstName(fName);
cout << "Please enter your last name: ";
cin >> lName;
inputInfo.setLastName(lName);
cout << "You are now registered as: " << inputInfo.getFirstName() << " " << inputInfo.getLastName();
}
void UserInfo::displayProfile()
{
UserInfo inputInfo;
cout << "Profile Information:" << endl;
cout << "Name: " << inputInfo.getFirstName() << " " << inputInfo.getLastName();
}
void UserInfo::setFirstName(string fName)
{
_firstName = fName;
}
string UserInfo::getFirstName()
{
return _firstName;
}
void UserInfo::setLastName(string lName)
{
_lastName = lName;
}
string UserInfo::getLastName()
{
return _lastName;
}
project02.h (header file)
#ifndef PROJECT02_H
#define PROJECT02_H
using namespace std;
class UserInfo
{
public:
string getFirstName();
void setFirstName(string first);
string getLastName();
void setLastName(string last);
int getBirthYear();
void setBirthYear(int year);
string getCurrentCity();
void setCurrentCity(string city);
string getOccupation();
void setOccupation(string occ);
void setUserInfo();
void displayProfile();
private:
string _firstName;
string _lastName;
int _birthYear;
string _currentCity;
string _occupation;
};
#endif // PROJECT02_H
project02main.cpp (main file)
#include <iostream>
#include "project02.h"
using namespace std;
int main()
{
UserInfo inputInfo;
inputInfo.setUserInfo();
return 0;
}
Now the question is: is there an alternative to repeatedly defining the object "UserInfo inputInfo;" each time for a different function in the implementation file?
Don't create objects of the same data type within methods of that datatype at this point - simply call setFirstname() and getFirstname() to use methods that will modify the same object that you are currently using.
Whenever I try to make an object and call a function on it, it doesn't seem to work.
I have no idea why, since I don't seem to have errors too.
I have searched around on here regarding constructors and the toString-method, but haven't find anything that worked.
I have tried to edit (distinct) the members in the constructor members,
Tried to rewrite the toString method.
Tried to make local object (with no pointer).
But it doesn't return me the things in the object that I created when calling the constructor.
Where does the problem situate in this problem?
Here is my code:
.h file:
#pragma once
#include "stdafx.h"
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
class Store{
private:
int id;
string name;
string adress;
string telephone;
string btwNumber;
public:
int getId();
void setId(int);
string getName();
void setName(string);
string getAdress();
void setAdress(string);
string getTelephone();
void setTelephone(string);
string getBtwNumber();
void setBtwNumber(std::string);
string toString();
Store(int, string, string , string, string);
};
.cpp file:
// Store.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Store.h"
Store::Store(int idnum, string nameS, string adreS, string telephonE, string btwnummeR){
idnum = id;
nameS = name;
adreS = adress;
telephonE = telephone;
btwnummeR = btwNumber;
}
int Store::getId()
{
return id;
}
void Store::setId(int id){
this->id = id;
}
string Store::getName(){
return naam;
}
void Store::setName(string name){
this->naam = naam;
}
string Store::getTelephone(){
return telephone;
}
void Store:setTelephone(string telephone){
this->telephone = telephone;
}
string Store::getBtwNumber()
{
return btwNumber;
}
void Store::setBtwNumber(string btwNumber){
btwNumber = btwNumber;
}
string Store::getAdress(){
return adress;
}
void Store::setAdress(string adress){
this->adress = adress;
}
string Store::toString(){
stringstream s;
s << "Id: " << id << endl;
s << "Naam: " << name << endl;
s << "Adres: " << adress << endl;
s << "Telefoonnummer: " << telephone << endl;
s << "BTWnummer: " << btwNumber << endl;
return s.str();
}
int _tmain(int argc, _TCHAR* argv[])
{
Store *test = new Store (4, "Test", "test", "test", "test");
test->toString();
system("Pause");
return 0;
}
Your constructor is inversed: you are assigning member variables to constructor arguments and not vice versa.
nameS = name;
Should be
name = nameS;
And so on
The method toString does work, but it won't magically decide to output its return value to screen. You'll have to do it yourself:
std::cout << test->toString() << std::endl;
You'll need to add #include <iostream> on top of your cpp file.