no matching function for call to vector .. found C++ - c++

I have went over code about 5 times, i am learning c++ and according to me everything should work however that's not happening. Error makes no sense, i mean vector suppose to have "at" function..
I get error:
In function 'void lookup(class vector<Customer,_default_alloc_template<false,0> > &, class string)': findByZipCode.cpp:30: no matching function for call to 'vector<Customer,_default_alloc_template,0> >::at (int)
findByZipCode.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include "customer.h"
using namespace std;
Name readRestOfName(istream &in,string f) {
string g;
getline(in,g);
Name abc (g,f);
return abc;
}
Address readAddress(istream &in) {
string street, city, state, zip;
getline(in, street);
getline(in, city);
getline(in, state);
getline(in, zip);
Address abc (street, city, state, zip);
return abc;
}
void lookup(vector<Customer>& v, string zip) {
// COMPLETE
vector <int> matches;
cout<<v.at(2).getName()<<endl; <<<---------------error here!!!
}
int main() {
// open customerdata file
ifstream in("customerdata");
vector<Customer> v;
string f;
while (getline(in,f)) {
Name n = readRestOfName(in,f);
Address a = readAddress(in);
Customer abc(n,a);
v.push_back(abc);
}
in.close();
string zip;
while(!cin.fail()){
cin>>zip;
lookup(v,zip);
}
return 0;
}
customer.cpp and customer.h
.cpp
#include <iostream>
#include "customer.h"
#include <string>
using namespace std;
string Customer::getName() {return n.getFamily() + ", " + n.getGiven();}
string Customer::getAddress() {return a.getStreet() + " " + a.getCity() + " "+ a.getState()+" "+a.getZip();}
string Customer::getZip() {return a.getZip();}
.h
#ifndef CUSTOMER_H
#define CUSTOMER_H
#include <string>
#include <iostream>
#include "name.h"
#include "address.h"
class Customer {
private:
Name n;
Address a;
public:
Customer(Name b,Address c): n(b), a(c) {}
std::string getName();
std::string getAddress();
std::string getZip();
};
#endif

Related

C++ - initializing static vector of pointers causes undefined reference

I'm trying to create a function below in my CreateReport class called load() that copies all the records (data) from my graduate.dat file into my static vector of Record pointers called primaryCollection. I created a Record class with variables that make up each Record, and in my load() function in createReport.cc I attempted to read each line in the file, create a Record object with each line, add it to my vector, and then print everything in primaryCollection.
The problem is every time I attempt to use primaryCollection, I keep getting the error:
CreateReport.o: In function `CreateReport::CreateReport()':
CreateReport.cc:(.text+0x43): undefined reference to `CreateReport::primaryCollection'
CreateReport.o: In function `CreateReport::load()':
CreateReport.cc:(.text+0x2ac): undefined reference to `CreateReport::primaryCollection'
CreateReport.cc:(.text+0x31d): undefined reference to `CreateReport::primaryCollection'
CreateReport.cc:(.text+0x32f): undefined reference to `CreateReport::primaryCollection'
I get 4 undefined references for the 4 times I mention primaryCollection in createReport.cc. I'm not sure if I'm initializing primaryCollection correctly and if that is whats causing these undefined references. I don't know if this is relevant to my problem, but CreateReport is also an abstract class and has a few subclasses called ReportOne, ReportTwo, etc.
primaryCollection is supposed to be a static vector of Record pointers and I'm also not allowed to use std::map for this task.
I would appreciate any help with this issue. I looked at this post Undefined reference to static variable c++ but I still don't really understand what to do. I'm not allowed to make global variables and I'm dealing with a collection rather than a single variable.
My graduate.dat file is formatted like below in the format < year province degree >
2000 AB Bachelor's
2005 AB Bachelor's
2005 MB College
Each line basically represents a Record. So the first record here is 2000 AB Bachelor's
EDIT: So I made changes to my code based on the comments by adding the line vector<Record*> CreateReport::primaryCollection; above my constructor, but it gives me the error:
CreateReport.cc:13:34: error: conflicting declaration ‘std::vector<Record*> CreateReport::primaryCollection’
vector<Record*> CreateReport::primaryCollection;
^~~~~~~~~~~~~~~~~
In file included from CreateReport.cc:5:0:
CreateReport.h:23:33: note: previous declaration as ‘std::vector<Record*>* CreateReport::primaryCollection’
static std::vector<Record*>* primaryCollection; //STL vector of record pointers
^~~~~~~~~~~~~~~~~
CreateReport.cc:13:34: error: declaration of ‘std::vector<Record*>* CreateReport::primaryCollection’ outside of class is not definition [-fpermissive]
vector<Record*> CreateReport::primaryCollection;
Any ideas how to fix this?
Record.h
#ifndef RECORD_H
#define RECORD_H
#include <iostream>
#include <string>
class Record{
public:
Record(int = 0, string = "", string = "");
~Record();
private:
int year;
string province;
string degree;
};
#endif
Record.cc
#include <iostream>
#include <string>
using namespace std;
#include "Record.h"
Record::Record(int i1, string s1, string s2) : year(i1), province(s1), degree(s2){}
Record::~Record(){}
CreateReport.h
#ifndef CREATEREPORT_H
#define CREATEREPORT_H
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include "Record.h"
class CreateReport{
public:
CreateReport();
static void load();
protected:
static vector<Record*> primaryCollection; //STL vector of record pointers
};
#endif
CreateReport.cc
#include <iostream>
using namespace std;
#include <string>
#include "CreateReport.h"
vector<Record*> CreateReport::primaryCollection;
CreateReport::CreateReport(){
}
void CreateReport::load(){
int year;
string province, degree;
ostream_iterator<Record*> outItr(cout);
ifstream infile("graduate.dat", ios::in);
if (!infile) {
cout << "Error: could not open file" << endl;
exit(1);
}
while (infile >> year >> province >> degree) { //as long as were not at end of file
Record* record = new Record(year, province, degree); //create Record object with this data
primaryCollection->push_back(record); //undefined reference
}
cout<<endl<<"List of Records:"<<endl;
copy(primaryCollection->begin(), primaryCollection->end(), outItr); //2 undefined references
}
Second version using `Record*` for `std::vector primaryCollection`.
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
class Record{
public:
Record(int = 0, string = "", string = "");
~Record()=default;
friend std::ostream& operator<<(std::ostream&, const Record&);
private:
int year;
string province;
string degree;
};
// **** output overload for Record ***********
std::ostream& operator<<(std::ostream& os, const Record& rd)
{
os << "year = " << rd.year << " prov = " << rd.province << " degree = " << rd.degree << std::endl;
return os;
}
// ****** end of output overload **************
Record::Record(int i1, string s1, string s2) : year(i1), province(s1), degree(s2){}
//end of Record.cc
//
class CreateReport{
public:
CreateReport() = default;
void load();
protected:
static vector<Record*> primaryCollection; //STL vector of record pointers
};
//***************** you need this line ********************
std::vector<Record*> CreateReport::primaryCollection;
//*********************************************************
void CreateReport::load(){
int year;
string province, degree;
ifstream infile("graduate.dat", ios::in);
if (!infile) {
cout << "Error: could not open file" << endl;
exit(1);
}
while (infile >> year >> province >> degree) {
Record *a = new Record(year, province, degree);
primaryCollection.push_back( a );
}
cout<<endl<<"List of Records:"<<endl;
for (int i = 0; i<primaryCollection.size(); ++i ) std::cout << *primaryCollection[i];
}
int main()
{
CreateReport mime;
mime.load();
}
Three major problems:
Using std::vector<*Record> cause many un-necessary difficulties;
For static member vector, a extra definition outside the class is necessary.std::vector<Record> CreateReport::primaryCollection;. This erases the undefined error message.
Using copy to std::cout doesn't work, it provide no method of printing Record. Suggest to write a output overload.
Based on these, I provide a version as follows (mixed all headers together.)
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
class Record{
public:
Record(int = 0, string = "", string = "");
~Record()=default;
friend std::ostream& operator<<(std::ostream&, const Record&);
private:
int year;
string province;
string degree;
};
// **** output overload for Record ***********
std::ostream& operator<<(std::ostream& os, const Record& rd)
{
os << "year = " << rd.year << " prov = " << rd.province << " degree = " << rd.degree << std::endl;
return os;
}
// ****** end of output overload **************
Record::Record(int i1, string s1, string s2) : year(i1), province(s1), degree(s2){}
//end of Record.cc
//
class CreateReport{
public:
CreateReport() = default;
void load();
protected:
static vector<Record> primaryCollection;
};
//***************** you need this line ********************
vector<Record> CreateReport::primaryCollection;
//*********************************************************
void CreateReport::load(){
int year;
string province, degree;
ifstream infile("graduate.dat", ios::in);
if (!infile) {
cout << "Error: could not open file" << endl;
exit(1);
}
while (infile >> year >> province >> degree) {
primaryCollection.push_back( Record(year, province, degree) );
}
cout<<endl<<"List of Records:"<<endl;
for (int i = 0; i<primaryCollection.size(); ++i ) std::cout << primaryCollection[i];
}
int main()
{
CreateReport mime;
mime.load();
}

c++ Class containing vectors of other Classes

I am trying to make a program that has "Class University" that contains a vector for "class Department" [vector Departments]. Inside of the "class Department" under public I have two constructors and a print function.
In my University.cpp, I am trying to make a function that adds a new object Department and puts it into the vector Departments in "University". I believe I am doing this wrong because I am trying to call the constructor for Department() in the University function and I am getting this error message:
$ g++ University.cpp
/usr/li`enter code here`b/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
/tmp/cc6XExwc.o: In function `University::CreateNewDepartment(std::string, std::string, long)':
University.cpp:(.text+0x7d): undefined reference to `Department::Department(std::string, std::string, long)'
collect2: error: ld returned 1 exit status
Class Departments has the following private variables (long id, string name, string location, long chairID). Do I need to use a set() function to create the object, and if so how would I go about doing this?
University.h below
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
using namespace std;
#include "Person.h"
#include "Student.h"
#include "Faculty.h"
#include "Department.h"
#include "Course.h"
class University
{
protected:
vector<Department> Departments;
vector<Student> Students;
vector<Course> Courses;
vector<Faculty> Faculties;
public:
University();
~University();
bool CreateNewDepartment(string depName, string depLoc, long depChairId);
University.cpp below
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
using namespace std;
#include "University.h"
bool University::CreateNewDepartment (string n, string l, long c)
{
if ( (c != 0) && (!validFaculty (c) ))
return false;
Department D (n, l, c);
Departments.push_back (D);
return true;
}
Department.h
#ifndef DEPARTMENT_H
#define DEPARTMENT_H
#include <string>
#include <iostream>
using namespace std;
class Department
{
friend class University;
protected:
long id;
string name;
string location;
long chairId;
static long nextDepartId;
public:
Department();
Department(string n, string l, long c);
void Print() const;
};
#endif
Department.cpp
#include "Department.h"
using namespace std;
#include <string>
long Department::nextDepartId;
Department::Department()
{
id = chairId = 0;
name = location = " ";
}
Department::Department(string n, string l, long c)
{
name = n;
location = l;
chairId = c;
}
void Department::Print() const
{
cout << "Name: " << name << endl;
cout << "Id: " << id << endl;
cout << "Location: " << location << endl;
cout << "Chair id: " << chairId << endl;
}
As said in the comments by #SteveHolodnak and #M.M., you have two undefined reference problems here.
No main function.
Undefined reference to Department::Department()
Using g++ main.cpp University.cpp Department.cpp where main.cpp is your file with your main function should solve all your problems.

Trying to call constructor, receiving "no matching function call" compilation error

I am currently working on creating a program called OOP head of students. the program is supposed to open up a file, read the information, send the information through classes and and properly display the results. I am currently creating information manually from main as a test.
Here is my code:
main.cpp
#include <iostream>
#include <string>
#include <fstream>
#include "School_Academics.h"
using namespace std;
int main(){
School_Academics n;
Name name = Name("jack", "lopez");
Address address = Address("123 some street", "Apt. A", "somecity", "somestate","somezipcode");
Date dob = Date("01", "12", "1990");//date of birth
Date dog = Date("07", "11", "2014");//date of graduation
School_Academics acad = School_Academics("3.64", "25", dog);
cout<<n.getStudent()<<endl;
Student n2 = Student(name, address, dob, acad);
cout<<n2.getStudent();
}
Student.h
#ifndef STUDENT_H_INCLUDED
#define STUDENT_H_INCLUDED
#include "Address.h"
#include "School_Academics.h"
#include "Name.h"
#include "Date.h"
using namespace std;
class Student{
private:
Name name;
Address address;
Date dob;
School_Academics acad;
public:
Student();
Student(Name name, Address address, Date dob, School_Academics acad);
string getStudent();
};
#endif // STUDENT_H_INCLUDED
Student.cpp
#include <string>
#include <iostream>
#include <cstdlib>
#include <fstream>
#include "Student.h"
using namespace std;
Student::Student(){
name = Name("","");
address = Address("","","","","");
dob = Date("0","0","0");
acad = School_Academics("0","0","0");
}
Student::Student(Name name, Address address, Date dob, School_Academics acad){
Student::name = name;
Student::address = address;
Student::dob = dob;
Student::acad = acad;
}
string Student::getStudent(){
name.getName();
address.getAddress();
acad.getSchool_Academics();
dob.getDate();
}
School_Academics.cpp
#include <string>
#include <iostream>
#include <cstdlib>
#include <sstream>
#include "School_Academics.h"
School_Academics::School_Academics(){
GPA = "0";
credits = "0";
Date DOG = Date("0","0","0");
}
School_Academics:: School_Academics(string GPA, string credits, Date DOG){
School_Academics::GPA = GPA;
School_Academics::credits = credits;
School_Academics::DOG = DOG;
}
string School_Academics::getSchool_Academics(){
stringstream ss;
ss <<"School Academics: GPA: "<<GPA<<" credits: "<<credits<<" Graduate: "<<DOG.getDate()<<endl;
return ss.str();
}
Date.cpp
#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>
#include "Date.h"
using namespace std;
Date::Date(){
day = "0";
month = "0";
year = "0";
}
Date::Date(string day, string month, string year){
Date::day = day;
Date::month = month;
Date::year = year;
}
string Date::getDate(){
stringstream ss;
ss <<"Date: "<< day << "/" << month << "/" << year << endl;
return ss.str();
}
The Problem:
I am currently having problems sending information from the main.cpp and passing it through the Student.cpp and getting the class Student{}; to get the information from the Date.cpp.
Error Received:
|13|error: no matching function for call to 'School_Academics::School_Academics(const char [1], const char [1], const char [1])'|
If you could please help me resolve the error. Thank you in advance.
The error comes from your student ctor where you have the following
acad = School_Academics("0","0","0");
The School_Academics ctor with 3 char is not defined. This cause the error.
You probably meant to pass the date you created just above:
Student::Student(){
name = Name("","");
address = Address("","","","","");
dob = Date("0","0","0");
acad = School_Academics("0","0",dob);
// or acad = School_Academics("0","0",date());
}

creating a new customer-endless compile errors using Visual Studio compile

guys what am i doing wrong here i need to create a new customer using a int main to call it, code below.....visual studio compiler just has endless errors can anyone offer a fix? thanks so much. hopefully this is enough detail...
#include <iostream>
#include <iomanip>
#include <string>
#define CUSTOMER_H
#indef CUSTOMER_H
using namespace std;
struct customer
{
string name;
string pin;
string user_id;
};
int main ()
{customer* CreateCustomer(const string& name, const string& id, const string& pin);
return new customer{ name, id, pin };
cout << new customer << endl; }
{
customer* Mary = CreateCustomer("Mary Jones", "235718", "5074");
}
return customer;
}
I will but this will be the last time. aurisdante was correct you do need to get a book on C++ programming if you are going to pursue programming. My initial response was a) because I remember when I was starting out. And B) to give you something that would at least compile. So, from here on out it is up to you to go forth and conquer...
#include <iostream>
#include <iomanip>
#include <string>
#ifndef CUSTOMER_H //CUSTOMER_H not defined
#define CUSTOMER_H //define it
#endif // CUSTOMER_H
using namespace std;
struct customer
{
string name;
string pin;
string user_id;
};
//You have to do this after you prototype the object so the complier knows what the object is
customer* CustomerCreator();
customer* CustomerCreator(string name, string pin, string u_Id);
int main()
{
customer* Customer1 = new customer { "Mary Jones", "235718", "5074" };
cout << Customer1->name << Customer1->pin << endl;
customer* Customer2 = CustomerCreator();
Customer2->name = "Put name here";
Customer2->pin = "0U812";
Customer2->user_id = "AnotherNumberGoesHere";
customer* Customer3 = CustomerCreator("William Shatner", "UCC-1", "HMFIC");
//this creates a break point
_asm int 3;
return 1;
}
//this just creates and returns an object custmer
customer* CustomerCreator()
{
customer* Customer = new customer();
return Customer;
}
customer* CustomerCreator(string name, string pin, string u_Id)
{
customer* Customer = new customer{ name , pin, u_Id};
return Customer;
}
You might want to try something like this:
#include <iostream>
#include <iomanip>
#include <string>
#ifndef CUSTOMER_H //CUSTOMER_H not defined
#define CUSTOMER_H //define it
#endif // CUSTOMER_H
using namespace std;
struct customer
{
string name;
string pin;
string user_id;
};
int main()
{
customer* CreateCustomer = new customer { "Mary Jones", "235718", "5074" };
cout << CreateCustomer->name << CreateCustomer->pin << endl;
//this creates a break point
_asm int 3;
return 1;
}

non-const lvalue reference to type cannot bind error

Im trying to create a very simple VCard but im getting a non-const lvalue reference to type cannot bind error in my main.cpp and can't figure this out. the problem line is.....
vc->createVCard("JasonSteindorf.vcf", &p1);
//Person.h
#ifndef JASONSTEINDORF_PERSON_H
#define JASONSTEINDORF_PERSON_H
#include <string>
using std::string;
namespace JasonSteindorf{
class Person{
public:
Person();
Person(string firstName,string lastName,string phoneNumber,string email)
: firstName(firstName), lastName(lastName), phoneNumber(phoneNumber), email(email){}
inline string getFirstName(){ return firstName; }
inline string getLastName(){ return lastName; }
inline string getPhoneNumber(){ return phoneNumber; }
inline string getEmail(){ return email; }
private:
string firstName, lastName, phoneNumber, email;
};
}
#endif
//VCard.h
#ifndef JASONSTEINDORF_VCARD_H
#define JASONSTEINDORF_VCARD_H
#include "Person.h"
#include <string>
using std::string;
namespace JasonSteindorf{
class VCard{
public:
void createVCard(string fileName, Person &p);
string getVCard(Person &p);
};
}
#endif
//VCard.cpp
#include "VCard.h"
#include <fstream>
using std::ofstream;
#include <string>
using std::string;
#include <sstream>
#include <iostream>
using std::ostringstream;
using namespace JasonSteindorf;
//Writes the VCard to a file
string getVCard(Person &p){
ostringstream os;
os << "BEGIN:VCARD\n"
<< "VERSION:3.0\n"
<< "N:" << p.getLastName() << ";" << p.getFirstName() << "\n"
<< "FN:" << p.getFirstName() <<" " << p.getLastName() << "\n"
<< "TEL:TYPE=CELL:" << p.getPhoneNumber() << "\n"
<< "EMAIL:" << p.getEmail() << "\n"
<< "URL:" << "http://sorcerer.ucsd.edu/html/people/jason.html" << "\n"
<< "REV:20110719T195243Z" << "\n"
<< "END:VCARD\n";
return os.str();
}
//Returns a string containing the VCard format
void createVCard(string fileName, Person &p){
string vCard = getVCard(p);
ofstream outputFile("/Users/jsteindorf/Desktop/" + fileName);
outputFile << vCard;
}
//main.cpp
#include "Person.h"
#include "VCard.h"
#include <iostream>
using namespace JasonSteindorf;
int main(int argc, const char * argv[])
{
VCard *vc = new VCard();
Person *p1 = new Person ("Jason", "S", "858-555-5555", "js#ucsd.edu");
vc->createVCard("JS.vcf", &p1);
return 0;
}
You haven't defined the functions createVCard and getCard as member functions of VCard class.
Those are global functions. Use the scope resolution operator :: to define them as member functions of the class like
void Vcard::createVCard(string fileName,Person &p)
{
....
....
}
string Vcard::getVCard(Person &p)
{
....
....
}
And also your createVCard function accepts a reference to Person hence you will have to pass the object to the person not the address of pointer to the object (&p) nor address of the object (p) instead pass the object by de-referencing it like *p, hence the call would look like vc->createVCard("JS.vcf", *p1)