How to declare getters/setters for an array member variable [duplicate] - c++

This question already has answers here:
How do I write setters and getters for an array? (c++)
(3 answers)
Closed 3 years ago.
I'm trying to represent a Course with Students. The Students have information about their first and last names, age... And the Courses have a name and an array of 3 Students.
I'm getting an error when I try to define the getters and setters for the array.
Error (active) E0415 no suitable constructor exists to convert from "Student [3]" to "Student"
Error (active) E0137 expression must be a modifiable lvalue
Course.h
#pragma once
#include "Student.h"
#include "Teacher.h"
class Course
{
private:
string name;
Student students[3];
Teacher teacher;
public:
Course();
~Course();
void setName(string name);
string getName();
void setStudents(Student students[3]);
[3] Student getStudents();
};
Course.cpp
#include <iostream>
#include "Course.h"
#include "Student.h"
#include "Teacher.h"
using namespace std;
Course::Course() {}
Course::~Course()
{
}
void Course::setName(string name)
{
this->name = name;
}
string Course::getName()
{
return this->name;
}
void Course::setStudents(Student students[3])
{
/*for (int i = 0; i < 3; i++) {
this->students[i] = students[i];
}*/
//This way the set works
this->students = students;
}
[3]Student Course::getStudents()
{
return this->students;
}
I expect the output of the get to be the array of students.

A C style array cannot be copied, cannot be automatically assigned, and cannot be returned from a function.
Thankfully, the C++ standard library provides a thin wrapper class over C style arrays which implement all these operations. It’s called std::array and it can be used exactly like you’re trying to use C-style arrays.
#pragma once
#include "Student.h"
#include "Teacher.h"
#include <array>
class Course
{
private:
string name;
std::array<Student, 3> students;
Teacher teacher;
public:
Course();
~Course();
void setName(string name);
string getName();
void setStudents(std::array<Student, 3> students);
std::array<Student, 3> getStudents();
};

Related

error: request for member in which is of non-class type [duplicate]

This question already has answers here:
How to create an object in a form like this: ifstream in();
(1 answer)
Why can MyClass foo() access private default constructor? [duplicate]
(2 answers)
Trying to understand default constructors and member initialisatioon
(3 answers)
Closed 6 days ago.
error: request for member in which is of non-class type
main.cpp|17|error: request for member 'setName' in 'studentOne', which is of non-class type 'Student()'
the error is on the last line
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
using namespace std;
class Student
{
public:
Student(string name,string program,int age);
void setName(string);
void setProgram(string);
void setAge(int);
string getName();
string getProgram();
int getAge();
private:
string Name;
string Program;
int Age;
};
#endif // STUDENT_H
#include "Student.h"
#include <iostream>
using namespace std;
Student::Student(string name,string program,int age)
{
setName(name);
setProgram(program);
setAge(age);
}
void Student::setName(string name)
{
Name = name;
}
string Student::getName()
{
return Name;
}
void Student::setProgram(string program)
{
Program = program;
}
string Student::getProgram()
{
return Name;
}
void Student::setAge(int age)
{
Age = age;
}
int Student::getAge()
{
return Age;
}
#include <iostream>
#include "Student.h"
using namespace std;
int main()
{
string studentName;
string studentProgram;
int studentAge;
Student studentOne();
cout << "Welcome to the Student input Wizard";
getline(cin, studentName);
cout << endl;
studentOne.setName(studentName);
}
am trying to call the member functions of the class in the main function to input the data for the variable name the others and to print the output.
But every member function I try to call brings about that error, tried the suggestion that I should removes the braces at the end of the object but the the error shows up on that line too.

Vector not being populated when done through constructor

i'm assuming this is going to be a really simple fix but i've been staring at this code for so long my brains turned to mush.
For background, we've had to create a bank account program. I have a Account class and a Person class. My issue lies in the Person class. When a person gets instantiated it should automatically create an account object and push it into a vector. I also have a method that lets me add another account or delete one.
My issue is, when in my main class, the vector is coming up as 0 when using the v.size() method. I'll paste my code below, i'm very new to C++ and only just really been introduced to pointers so bare that in mind.
Person.cpp
#include "pch.h"
#include "Person.h"
#include <iostream>
using namespace std;
Person::Person()
{
Account acc1;
account.push_back(acc1);
}
void Person::newAccount() {
Account acc1;
account.push_back(acc1);
}
void Person::closeAccount(int index) {
cout << "Which account would you like to close?" << endl;
cin >> index;
account.erase(account.begin() + index);
}
Person::~Person()
{
}
Person.h
#pragma once
#include "Account.h"
#include <vector>
using namespace std;
class Person
{
public:
Person();
void newAccount();
void closeAccount(int index);
~Person();
private:
vector <Account> account;
};
Then in the main class I have:
#include "pch.h"
#include <iostream>
#include <iomanip>
#include <vector>
#include "Account.h"
#include "Person.h"
using namespace std;
int amount;
int years;
int balance;
vector <Account> accounts;
int main()
{
Person p;
Account a;
p.newAccount();
cout << accounts.size() << endl;
I left out the rest of the main class as that has nothing to do with this error but will post if needed.
Any help would be appreciated
The issue is that you are sampling the size of a global variable called
vector <Account> accounts;
This is not the same as the member variable you declared in your Person class called
vector <Account> account;
declared in your person class below:
class Person
{
public:
Person();
void newAccount();
void closeAccount(int index);
~Person();
private:
vector <Account> account;
};
These are two separate objects and your account appending is happening to your member variable within your Person Class. Change the output statement to report the size of the account vector of a Person instance through a function since the vector is private and it will report the correct size.
class Person
{
public:
Person();
void newAccount();
void closeAccount(int index);
~Person();
inline int GetAccountNum() const { return account.size(); }
private:
vector <Account> account;
};
int main()
{
Person p;
p.newAccount();
cout << p.GetAccountNum() << endl;
}
One popular coding convention C++ programmers use is to prefix member variables with m_ or m to avoid these kind of easy mistakes. It also can help to change the name of your vector to something other than just the plural form of the object.
Lastly, the Account object a, you are creating on the stack in the main function is not being used. This would produce a warning in most IDE's. If your intention was to add the account you created on the stack to your Person object's account vector, then you should create a function that accepts an account by reference and adds it to the member variable account vector for the Person object.
Follow the convention I listed above and it should help you avoid this mistakes in the future.

Undefined reference to Class::Class/Function (Beginner in OOP)

I have this annoying error;
Undefined Reference to Shape::Shape(...), Shape::getName(...), Shape::getAge(...)
My Main.cpp is this
#include <iostream>
#include <string>
#include "Bit.h"
using namespace std;
int main()
{
//simple assignment
string name;
int age;
cout<<"enter name: ";
cin>>name;
cout<<"enter age: ";
cin>>age;
Shape sh(name,age); //class declaration (i think here is the problem)
cout<<endl<<"name: "<<sh.getName();
cout<<endl<<"age: "<<sh.getAge();
return 0;
}
This is the Bit.h header
#include <iostream>
#include <string>
using namespace std;
#ifndef BIT_H
#define BIT_H
//creating class
class Shape{
string newName;
int newAge;
public:
//Default Constructor
Shape();
//Overload Constructor
Shape(string n,int a);
//Destructor
~Shape();
//Accessor Functions
string getName();
int getAge();
};
And finally, this is the Bit.cpp
#include "Bit.h"
//constructors and destructor
Shape::Shape(){
newName="";
newAge=0;
}
Shape::Shape(string n, int a){
newName=name;
newAge=age;
}
Shape::~Shape(){
}
string Shape::getName(){
return newName;
}
//getters
int Shape::getAge(){
return newAge;
}
I understand, that this might be a very simple problem/error, but I have been struggling for about 2 hours.
I suppose that the error is in the declaration od "sh" object, even if I declare it like this "Shape sh();" or "Shape sh;", there are still errors.
Thanks
EDIT. GNU GCC Compiler
EDIT2. Using Code Blocks (sorry for forgetting all these)
You're probably not compiling Bit.cpp, but only Main.cpp.
Considering that Bit.h, Bit.cpp and Main.cpp are in the same folder, here is how you should compile it :
g++ *.cpp
It will still not compile, as in the default constructor, you're trying to initialize name, and age which both don't exist.
You probably meant newAge, and newName?
In the other Constructor, name and age also don't exist, you probably meant n, and a?

How to do you call a function inside separated class?

So, I tried to make this code :
#include <iostream>
using namespace std;
class BuckysClass{
public:
void setName(string x){
name = x;
}
string getName(){
return name;
}
private:
string name;
};
int main()
{
BuckysClass bo;
bo.setName("Buckingham Palace");
cout << bo.getName();
return 0;
}
BECOMING TO A SEPARATED CLASS like this :
#include "BuckysClass.h"
#include <iostream>
#include <string>
using namespace std;
int main (){
BuckysClass bo;
bo.setName("Buckingham Palace");
cout << bo.getName();
return 0;
}
==============
#ifndef BUCKYSCLASS_H
#define BUCKYSCLASS_H
class BuckysClass
{
public:
void setName(string x);
string getName();
private:
string name;
};
#endif // BUCKYSCLASS_H
=============
#include "BuckysClass.h"
#include <iostream>
#include <string>
using namespace std;
BuckysClass::BuckysClass()
{
}
void setName(string x){
name = x;
}
string getName(){
return name;
}
When I run the first code, I succeed,
but I got error when running the separated class code,
help me find out what's wrong ???
I tried to use different code,
but it seems I can't find the reason,
the closest reason I believe, is the main doesnt call the function on the separated class properly.
If you write this code:
void setName(string x){
name = x;
}
string getName(){
return name;
}
The compiler interprete it as two functions called setName and getName, it has no idea they are member functions of your BuckysClass class.
You have to precise it with the following syntax:
void BuckysClass::setName(string x){
name = x;
}
string BuckysClass::getName(){
return name;
}
Additionally, here you are defining a default constructor:
BuckysClass::BuckysClass()
{
}
But you didn't put it in the class prototype. You have to add it somewhere in the class prototype definition in your .h file, or your compiler won't recognize it:
class BuckysClass
{
public:
BuckysClass(); // Default constructor.
void setName(string x);
string getName();
private:
string name;
};

Defining a vector within a custom class

I'm trying to simply use a vector within one of my classes. When trying to access the vector it tells me that it's undefined (but I've defined it in my header).
I have two classes, Person and Dog. A person can own one or more dogs so I want to add each dog a person owns into an array. This should be real simple so this problem is really starting to get to me. Here's some code:
The class Person.cpp:
#include "Person.h"
#include "stdafx.h"
#include <iostream>
using namespace std;
Person::Person(string name, string address, int age)
:name(name),
address(address),
age(age)
{}
int Person::getAge(){
return age;
}
std::string Person::getDogInfo(int index){
}
void Person::addDog(string dogName, string breed){
dogCollection.push_back(Dog(dogName, breed));
}
std::vector<Dog> getDogs(){
return dogCollection; //dogCollection undefined error here
}
And here's Person.h:
#ifndef Person_H
#define Person_H
#include <vector>
#include "Dog.h"
using namespace std;
class Person{
public:
Person(string name, string address, int age);
string getName(){return name};
string getAddress(){return address};
void addDog(string dogName, string breed);
string getDogInfo(int index);
std::vector<Dog> getDogs();
int getAge();
private:
string name;
string address;
int age;
std::vector<Dog> dogCollection;
};
#endif
If you want to have a look at my dog classes I'll paste them as well:
Dog.cpp:
#include "stdafx.h"
#include <iostream>
#include "dog.h"
Dog::Dog(string dogName, string breed)
:dogName(dogName),
breed(breed){}
std::string Dog::Dog.getDogName(){
return dogName;
}
std::string Dog::Dog.getBreed(){
return breed;
}
and Dog.h:
#ifndef Dog_H
#define Dog_H
#include <iostream>
using namespace std;
class Dog{
public:
Dog(std::string dogName, std::string breed);
std::string getDogName();
std::string getBreed();
private:
std::string dogName;
std::string breed;
};
#endif
Also, I just want to add that this is no homework. I'm used to java and I'm only trying to learn some C++ since I need it for future work.
EDIT: Updated the code
std::vector<Dog> dogCollection; // here im defining dogCollection, no error here!
There actually is an problem here - class Dog isn't known to the compiler at this point.
You can solve this by either including Dog.h before Person.h in Person.cpp, or better add an #include "Dog.h" at the top of Person.h.
This is incorrect (and unrequired):
dogCollection = new std::vector<Dog>; // Remove this line.
as dogCollection is not a std::vector<Dog>*.
This is also incorrect:
void Person::addDog(string dogName, string breed){
Dog *newDog = new Dog(dogName, breed);
dogCollection.push_back(newDog);
}
as dogCollection contains Dog instances, not Dog*. Change to:
void Person::addDog(string dogName, string breed){
dogCollection.push_back(Dog(dogName, breed));
}
There is a problem with all of constructors:
Person::Person(string name, string address, int age){
name=name;
address=address;
age=age;
}
This is assigning the argument name to itself: it is not assigning to the member name. Same for address and age and similarly for the constructors of the other classes. Use initializer list:
Person::Person(string name, string address, int age) :
name(name),
address(address),
age(age)
{}
This method does not return a std::string:
string Person::getDogInfo(int index){
}
EDIT:
Missing class qualifier:
std::vector<Dog> getDogs(){
return dogCollection; //dogCollection undefined error here
}
means this is just a free function, with no association to class Person and therefore no access to dogCollection.
Change to:
std::vector<Dog> Person::getDogs(){
return dogCollection;
}
There are several problems with your code, and most of the other answers have pointed them out - mostly regarding the use of new when it should not be used. (Are you a C# programmer, moving to C++?)
However, there are problems with the #include directives as well. As #Bo mentioned, since Person uses Dog, you should include that header in Person.h. But Person also uses vector so that header should be included there as well. So Person.h should start with...
#include <vector>
#include "Dog.h"
Then in Person.cpp you don't have to include those files.
As a general rule (you can learn about "forward declaration" later), any types referenced in a header should be #included in that header.