jeudi 8 septembre 2022

How to translate Python code to C++ code? [duplicate]

I have the next py file 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.start2 = Room()
        self.insert = Person()
        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 am trying to pass this code to C++ code:

#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 *start2;
    Person *insert;
    bool exist2;

    StudyRoom(){
        this->start2 = new Room();
        this->insert = new Person();
        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;
    }
};

But when I try to compile I just get the next error in console:

books.cpp: In member function 'int StudyRoom::existItem(Person*, Room*)':
books.cpp:47: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'}
   47 |         if (keyC->age < root->keysC[0].age)//error #1
      |                                        ^~~
books.cpp:55: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'}
   55 |             while (keyC->age < root->keysC[value - 1].age && value > 1)//error #2
      |                                                       ^~~

That is why I am asking for help, because I don´t know what could be my error. Maybe is because of the parameters in existItem method? Or because I am using vectors in C++? Or should I use -> instead of . in (keyC->age < root->keysC[0].age line? I think I have tried all this, but anything work for me. I hope you can help me with that, thanks.

Aucun commentaire:

Enregistrer un commentaire