I have books.py
:
class Person(object):
def __init__(self, user=None, age=None):
self.user = user
self.age=age
class Room(object):
def __init__(self, spaces=[0,0,0,0,0], keysC=[0,0,0,0], accounts=0):
self.spaces = spaces
self.keysC = keysC
self.accounts = accounts
class StudyRoom(object):
def __init__(self):
self.start = Room()
self.start2 = Room()
self.insert = Person()
self.link = Room()
self.piv = False
self.exist = False
self.exist2 = False
def existItem(self, keyC, root):
value =0
if(keyC.age < root.keysC[0].age):
self.exist2 = False
value = 0
else:
value = root.accounts
while (keyC.age < root.keysC[value - 1].age and value > 1):
value-=1
return value
And I want to convert books.py
to C++ in books.cpp
file.
So I am trying it in the next way:
#include <iostream>
#include <string>
using namespace std;
#include <vector>
class Person
{
public:
string user;
int age;
Person(string user = nullptr, int age = 0){
this->user = user;
this->age = age;
}
};
class Room
{
public:
int accounts;
vector<int> spaces;
vector<int> keysC;
Room(vector<int> spaces = {0,0,0,0,0}, vector<int> keysC = {0,0,0,0}, int accounts = 0){
this->spaces = spaces;
this->keysC = keysC;
this->accounts = accounts;
}
};
class StudyRoom
{
public:
Room *start, *start2, *link;
Person *insert;
bool piv, exist, exist2;
StudyRoom(){
this->start = new Room();
this->start2 = new Room();
this->insert = new Person();
this->link = new Room();
this->piv = false;
this->exist = false;
this->exist2 = false;
}
int existItem(Person* keyC, Room* root){
int value = 0;
if (keyC->age < root->keysC[0]->age)//error #1
{
this->exist2 = false;
value = 0;
}
else
{
value = root->accounts;
while (keyC->age < root->keysC[value - 1]->age && value > 1)//error #2
{
value = 1;
}
}
return value;
}
};
Errors are in books.cpp
file in existItem
method. I am using vscode, and it show me the next errors before to compile:
Then when I try to compile, I get the next error in console:
books.cpp: In member function 'int StudyRoom::existItem(Person*, Room*)':
books.cpp:52:39: error: base operand of '->' is not a pointer
52 | if (keyC->age < root->keysC[0]->age)//error #1
| ^~
books.cpp:60:54: error: base operand of '->' is not a pointer
60 | while (keyC->age < root->keysC[value - 1]->age && value > 1)//error #2
| ^~
I tried to solve this by switch root->keysC[0]->age
to root->keysC[0].age
, and the same with the other error line, but I just get other errors in console:
books.cpp: In member function 'int StudyRoom::existItem(Person*, Room*)':
books.cpp:52:40: error: request for member 'age' in 'root->Room::keysC.std::vector<int>::operator[](0)', which is of non-class type '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
52 | if (keyC->age < root->keysC[0].age)//error #1
| ^~~
books.cpp:60:55: error: request for member 'age' in 'root->Room::keysC.std::vector<int>::operator[](((std::vector<int>::size_type)(value - 1)))', which is of non-class type '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
60 | while (keyC->age < root->keysC[value - 1].age && value > 1)//error #2
| ^~~
Right now I am not waiting for a specific output in console, I just want to my code to compile without errors, but as you can see what I have done does not work. I hope you can help me, thanks.