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

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();
}

Related

How would I use a friend method that is predefined in a header file?

Essentially, my Computer Science teacher is making me use the friend std::ostream method for output.. I have imported it into the header file as one can see below, but I have no clue how to use it in the student.cpp. Adding student::ostream did not work. How would I be able to use the header predefined method in my student.cpp
My header file
#pragma once
#include <iostream>
class student
{
public:
student();
std::string settingStudentName;
bool disiplineIssue();
// provided again so you learn this valuable override method for printing class data.
friend std::ostream& operator << (std::ostream& const student &);
private:
std::string studentName;
bool hasDisciplineIssue;
};
Student.cpp
#include "student.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
student::student()
{
}
bool student::disiplineIssue()
{
// Max the random integer can go
int max = 100;
srand(time(0));
int random = rand()%max;
// bool variable for returning
bool True_or_False = false;
if (random <= 5)
{
True_or_False = true;
}
return True_or_False;
}
ostream& operator<< (ostream& output, const student& aStudent) {
output << aStudent.studentName << " ";
if (aStudent.hasDisciplineIssue) {
output << "has ";
}
else {
output << "doesn't have ";
}
output << "a discipline issue";
return output;
}
Edit:
When I do not have student:: in front, ostream works but if I add student:: in front, it says it can not resolve the symbol. I am not sure if the one without student:: is using the one I defined in the header file.
I would proceed in the following manner:
add body and declaration parameters to function settingStudentName in both header and main file;
insert correct dependancies (#include);
add a main() driver function;
also, use correct syntax (friend std::ostream& operator << (std::ostream&, const student &);) lacked the comma , separator between the function arguments.
A MWE:
#ifndef STUDENT_H_INCLUDED
#define STUDENT_H_INCLUDED
#include <iostream>
#include <string>
class student
{
public:
student();
std::string settingStudentName(const std::string&);
bool disiplineIssue();
// provided again so you learn this valuable override method for printing class data.
friend std::ostream& operator << (std::ostream&, const student &);
private:
std::string studentName;
bool hasDisciplineIssue;
};
#endif // STUDENT_H_INCLUDED
#include "student.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
student::student()
{
}
string student::settingStudentName(const string& input)
{
studentName = input;
return input;
}
bool student::disiplineIssue()
{
// Max the random integer can go
int max = 100;
srand(time(0));
int random = rand()%max;
// bool variable for returning
bool True_or_False = false;
if (random <= 5)
{
True_or_False = true;
}
return True_or_False;
}
ostream& operator<< (ostream& output, const student& aStudent) {
output << aStudent.studentName << " ";
if (aStudent.hasDisciplineIssue) {
output << "has ";
}
else {
output << "doesn't have ";
}
output << "a discipline issue";
return output;
}
int main()
{
student Jack;
Jack.settingStudentName("Jack");
Jack.disiplineIssue();
cout << Jack << endl;
return 0;
}
Here is the output:
Jack has a discipline issue
And here is a compiled version you can play with https://wandbox.org/permlink/mF49xQxkXs3M7n0M

Getting syntax errors from a header included with GCC

I am using Sleep from Windows.h, and when I go to compile it is giving me these errors. I tried googling them and found nothing. Also i wouldn't expect there to be issues like this from the include files from GCC. Can anyone tell me what these errors could be from.
edit: with a lot of this code it is unfinished, but wanted to get it tested partially. I mainly just wanted an idea of the type of screw up on my end to look for because I have no idea where to start.
>g++ -o test.exe main.cpp
In file included from C:/TDM-GCC-64/x86_64-w64-mingw32/include/windows.h:65:0,
from main.cpp:3:
C:/TDM-GCC-64/x86_64-w64-mingw32/include/excpt.h:14:8: error: expected unqualified-id before string constant
extern "C" {
^
In file included from C:/TDM-GCC-64/x86_64-w64-mingw32/include/windows.h:65:0,
from main.cpp:3:
C:/TDM-GCC-64/x86_64-w64-mingw32/include/excpt.h:128:18: error: expected '}' before end of line
C:/TDM-GCC-64/x86_64-w64-mingw32/include/excpt.h:128:18: error: expected unqualified-id before end of line
C:/TDM-GCC-64/x86_64-w64-mingw32/include/excpt.h:128:18: error: expected declaration before end of line
here is the main:
#include "program/skull_ascii.h"
#include "Player.h"
//#include "Building.h"
#include "Windows.h"
#include <iomanip>
#include <stdlib.h>
#include <string>
//prototypes
void intro();
void cityScene(Player*);
Player* getUserInfo();
void scroll(std::string text);
int main(){
intro(); //flashy ascii text intro
player* user = getUserInfo();
cityScene(user);
}
void intro(){ //not done tweaking
system("mode con:cols=81 lines=30");
system("color 02");
skull_title();
Sleep(1000);
for(int i = 1; i < 15; i++){
system("color 04");
Sleep(i*i / 2);
system("color 02");
Sleep(i*i);
}
system("color 04");
system("pause");
system("cls");
system("mode con:cols=120 lines=30");
}
Player* getUserInfo(){ //not done, just enough to test
player* user = new Player("test", 56);
return user;
}
void cityScene(Player* user){ //not close to finished, but shouldn't cause any issue
std::string prompt = "arbitrary text"; // will print out with other function
system("pause");
system("cls");
std::cout << "what do you want to do?: \n"
std::cout << "\t1.Just relax its not that important\n";
std::cout << "\t2.scavenge the apartment for items\n";
std::cout << "\t3.Leave immediately, there's no time left\n";
std::cout << ":";
}
void scroll(std::string text){
for(int i = 0; i < text.length(); i++){
std::cout << text[i];
Sleep(200);
if(i%45 == 0) std::cout << "\n";
}
}
the player class is a messy container atm, and isn't done
here is the player class:
#pragma once
#include <string>
#include <list>
#include <iostream>
#include "Item.h"
class Player{
std::string name;
int age;
std::list<Item> inventory;
public:
Player(std::string n, int a){
this->name = n;
this->age = a;
}
/* F-name: updateScreen()
params: none
return: void
purpos: updates the play screen with current player information
*/void update(){
system("cls"); // clear screen
std::cout << "NAME:" << name << " |\
AGE:XXX | \
WEAPONS: weapon 1, weapon2 | \
PACK_WEIGHT:XXXXXXXKgs | \
PARTY: name1, name2, name3, name4 | \
LOCATION: big city";
}
/*
F-name: addItem()
params: item object to add
return: void
purpos: adds an item to the inventory list
*/ void addItem(Item newItem){}
#pragma once
#include <iostream>
#include <string>
class Item{
std::string name;
std::string desc;
public:
Item(){
name = "void";
desc = "void";
}
Item(const Item& copy){
this->name = copy.name;
this->desc = copy.desc;
}
Item(std::string n, std::string d){
name = n;
desc = d;
}
~Item(){}
friend std::ostream& operator<< (std::ostream& out, const Item& thing){
out << thing.name;
return out;
}
void setName(std::string n){name = n;}
void setDesc(std::string d){desc = d;}
std::string getName(){return name;}
std::string getDesc(){return desc;}
};
The Player class definition in Player.h is not closed correctly. You forgot to add:
};
at the end. Right now, this causes the next header you include to cause a compilation error.

C++ clang linker command failed with exit code 1 [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 4 years ago.
My professor gave me two class header and .cpp files to build on. When I include these in main, they work fine. Whenever I just use his files, I get linker errors with clang and xcode.
Here's the error:
shannigan#mbp-007100 inheritance (master) $ make main
c++ main.cpp -o main
Undefined symbols for architecture x86_64:
"SavitchEmployees::SalariedEmployee::SalariedEmployee()", referenced from:
_main in main-0d7e27.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main] Error 1
Here's my main:
#include "employee.h"
#include "salariedemployee.h"
#include <string>
#include <cstdlib>
#include <iostream>
using namespace SavitchEmployees;
using namespace std;
int main() {
cout << "Do I run?" << endl;
SalariedEmployee sam;
return 0;
};
The header file for Employee:
//This is the header file employee.h.
//This is the interface for the class Employee.
//This is primarily intended to be used as a base class to derive
//classes for different kinds of employees.
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
using std::string;
namespace SavitchEmployees
{
class Employee
{
public:
Employee( );
Employee(const string& theName, const string& theSsn);
string getName( ) const;
string getSsn( ) const;
double getNetPay( ) const;
void setName(const string& newName);
void setSsn(const string& newSsn);
void setNetPay(double newNetPay);
void printCheck( ) const;
protected:
string name;
string ssn;
double netPay;
};
}//SavitchEmployees
#endif //EMPLOYEE_H
The CPP file for main:
//This is the file: employee.cpp
//This is the implementation for the class Employee.
//The interface for the class Employee is in the header file employee.h.
#include <string>
#include <cstdlib>
#include <iostream>
#include "employee.h"
using std::string;
using std::cout;
namespace SavitchEmployees
{
Employee::Employee( ) : name("No name yet"), ssn("No number yet"), netPay(0)
{
//deliberately empty
}
Employee::Employee(const string& theName, const string& theNumber)
: name(theName), ssn(theNumber), netPay(0)
{
//deliberately empty
}
string Employee::getName( ) const
{
return name;
}
string Employee::getSsn( ) const
{
return ssn;
}
double Employee::getNetPay( ) const
{
return netPay;
}
void Employee::setName(const string& newName)
{
name = newName;
}
void Employee::setSsn(const string& newSsn)
{
ssn = newSsn;
}
void Employee::setNetPay (double newNetPay)
{
netPay = newNetPay;
}
void Employee::printCheck( ) const
{
cout << "\nERROR: printCheck FUNCTION CALLED FOR AN \n"
<< "UNDIFFERENTIATED EMPLOYEE. Aborting the program.\n"
<< "Check with the author of the program about this bug.\n";
exit(1);
}
}//SavitchEmployees
SalariedEmployees header:
//This is the header file salariedemployee.h.
//This is the interface for the class SalariedEmployee.
#ifndef SALARIEDEMPLOYEE_H
#define SALARIEDEMPLOYEE_H
#include <string>
#include "employee.h"
using std::string;
namespace SavitchEmployees
{
class SalariedEmployee : public Employee
{
protected:
double salary;//weekly
public:
SalariedEmployee( );
SalariedEmployee (const string& theName, const string& theSsn,
double theWeeklySalary);
double getSalary( ) const;
void setSalary(double newSalary);
void printCheck( );
};
}//SavitchEmployees
#endif //SALARIEDEMPLOYEE_H
SalariedEmployee.cpp:
//This is the file salariedemployee.cpp
//This is the implementation for the class SalariedEmployee.
//The interface for the class SalariedEmployee is in
//the header file salariedemployee.h.
#include <iostream>
#include <string>
#include "salariedemployee.h"
using std::string;
using std::cout;
using std::endl;
namespace SavitchEmployees
{
SalariedEmployee::SalariedEmployee( ) : Employee( ), salary(0)
{
//deliberately empty
}
SalariedEmployee::SalariedEmployee(const string& newName, const string& newNumber,
double newWeeklyPay)
: Employee(newName, newNumber), salary(newWeeklyPay)
{
//deliberately empty
}
double SalariedEmployee::getSalary( ) const
{
return salary;
}
void SalariedEmployee::setSalary(double newSalary)
{
salary = newSalary;
}
void SalariedEmployee::printCheck( )
{
setNetPay(salary);
cout << "\n__________________________________________________\n";
cout << "Pay to the order of " << getName( ) << endl;
cout << "The sum of " << getNetPay( ) << " Dollars\n";
cout << "_________________________________________________\n";
cout << "Check Stub NOT NEGOTIABLE \n";
cout << "Employee Number: " << getSsn( ) << endl;
cout << "Salaried Employee. Regular Pay: "
<< salary << endl;
cout << "_________________________________________________\n";
}
}//SavitchEmployees
How can I get rid of these linker errors so I can focus on my actual code? Is there anything obvious wrong? The only thing I've changed was making the "private" variables protected.
I can't see the class named SalariedEmployee.
I think the main function should look like this.
int main() {
cout << "Do I run?" << endl;
Employee sam;
return 0;
};
You have to use Employee instead of SalariedEmployee

Strange characters when displaying the char array C++

I can't correctly display information. In the text displayed extra characters. Most likely a problem with the zero character but I was not able to eliminate.
First file: person.h Here I describe a simple class.
#ifndef PERSON_H_INCLUDED
#define PERSON_H_INCLUDED
#include <string>
using std::string;
class Person
{
private:
static const int m_iLIMIT = 25;
string m_sLname; // lastname
char m_cFname[m_iLIMIT]; // firstname
public:
Person () { m_sLname = ""; m_cFname[0] = '\0'; } // #1
Person(const string & sLn, const char * pcFn = "Adam"); // #2
// show lastname и firstname
void Show() const; // format: firstname lastname
void FormalShow() const; // format: lastname, firstname
};
#endif // PERSON_H_INCLUDED
Second file: p_functions.cpp Here I define class methods
#include "person.h"
#include <iostream>
#include <string>
#include <cstring>
using std::string;
using std::cout;
using std::cin;
using std::endl;
// #2
Person::Person(const string & sLn, const char * cFn)
{
m_sLname = sLn;
strcat(m_cFname, cFn);
}
void Person::Show() const
{
cout << "First format: " << m_cFname << ", " << m_sLname << endl;
}
void Person::FormalShow() const
{
cout << "Second format: " << m_sLname << ", " << m_cFname << endl;
}
Third file: main.cpp Here I am testing methods
#include <iostream>
#include "person.h"
using namespace std;
int main()
{
Person one;
Person two("Smith");
Person three("immanuel", "Kant");
one.Show();
one.FormalShow();
two.Show();
two.FormalShow();
three.Show();
three.FormalShow();
return 0;
}
This is the result I get
in the second constructor m_cFname[0] is not initialized with 0
use strcpy instead of strcat
and you will have problem with longer first names

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)