Couple of odd errors
Oct 11, 2016 at 12:45am UTC
I've been trying to put together some basic stuff in C++, but gotten stuck here.
Here's my main:
1 2 3 4 5 6 7 8 9
int main() {
PersonReg *reg= new PersonReg(10);
RegInput input;
std::string fileName = "Hoi" ;
input.ReadReg(*reg, fileName);
reg->print();
return 0;
}
Here's PersonReg:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#include <string>
#include <iostream>
#include "PersonReg.h"
PersonReg::PersonReg(int maxSize) {
this ->maxSize = maxSize;
personer = new Person[maxSize];
personer[0].setPerson("John" , "Trelleborg" );
personer[1].setPerson("Bob" , "Helsinki" );
}
void PersonReg::addPerson(Person *newPerson) {
int index = 0;
while (index < maxSize) {
if (personer[index].getName() == "" ) {
personer[index] = *newPerson;
return ;
}
index++;
}
std::cout << "Registry full!" << std::endl;
}
void PersonReg::removePerson(Person *removePerson) {
int index = 0;
while (index < maxSize) {
if (personer[index].getName() == removePerson->getName()
&& personer[index].getAdress() == removePerson->getAdress()) {
personer[index].clear();
return ;
}
index++;
}
}
Person* PersonReg::findPerson(char * query) {
size_t index = 0, index2 = 0;
while (index < maxSize) {
while (index2 < strlen(query) && query[index2] == personer[index].getName().at(index2))
{
if (query[index2+1] == '\0' ) {
return &personer[index];
}
index2++;
}
index++;
}
return NULL;
}
void PersonReg::print() {
int index = 0;
while (index < maxSize) {
if (personer[index].getName() != "" ) {
personer[index].print();
}
index++;
}
}
PersonReg::~PersonReg() {
delete personer;
}
.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#ifndef PERSONREG_H
#define PERSONREG_H
#include "Person.h"
#include "RegInput.h"
class PersonReg {
public :
Person *personer;
int maxSize;
PersonReg(int maxSize);
void addPerson(Person *newPerson);
void removePerson(Person *removePerson);
Person* findPerson(char * query);
void print();
~PersonReg();
};
#endif
RegInput:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
[#include <iostream>
#include <fstream>
#include <string>
#include "RegInput.h"
#include "PersonReg.h"
bool RegInput::ReadReg(PersonReg& reg, std::string fileName) {
std::string line;
std::ifstream myfile(fileName);
if (myfile.is_open()) {
while (std::getline(myfile, line)) {
while (line.length() == 0 && getline(myfile, line));
std::string name(line);
std::string adress;
getline(myfile, adress);
reg.addPerson(&Person(name, adress));
} myfile.close();
return true ;
}
else {
std::cout << "Unable to open file" ;
return false ;
}
}
]
RegInput.h
1 2 3 4 5 6 7 8 9 10
#ifndef REGINPUT_H
#define REGINPUT_H
#include "PersonReg.h"
class RegInput {
public :
bool ReadReg(PersonReg& reg, std::string fileName);
};
#endif
When compiled, I get "RegInput::ReadReg: function does not take 2 arguments" while it's executed with 2 arguments: bool ReadReg(PersonReg& reg, std::string fileName). The error points to the line 'input.ReadReg(*reg, fileName);' in the main method.
I also get 'syntax error: identifier 'PersonReg' on the line 'bool ReadReg(PersonReg& reg, std::string fileName);' in the RegInput header file even though "PersonReg.h" is included.
What am I doing wrong?
Last edited on Oct 11, 2016 at 12:47am UTC
Topic archived. No new replies allowed.