1

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: errors

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.

asker
  • 298
  • 2
  • 8
  • `keysC` is a vector of `int`s, not a vector of `Person*`. So why do you think `root->keysC[0]->age` should work ? – wohlstad Sep 08 '22 at 05:56
  • `keysC` is `vector` which means `keysC[0]` is `int` which means you can't apply `operator->` to it. – Jason Sep 08 '22 at 05:57
  • Have you ever run your Python code? In my opinion, the implementation of the `existItem` method itself has problems... – Mechanic Pig Sep 08 '22 at 06:01
  • 2
    `string user = nullptr` won't end well. `string user = ""` if you must have a default parameter. Better to just have a default constructor and a parameterized one IMO. May also want to read [Why should I prefer to use member initialization lists?](https://stackoverflow.com/questions/926752/why-should-i-prefer-to-use-member-initialization-lists) – Retired Ninja Sep 08 '22 at 06:02
  • 1
    Note also that `string user = nullptr` isn't great. In C++23, the `basic_string` constructor that converts from a `nullptr_t` will be explicitly deleted. But until then, it invokes undefined behavior because it turns into the `const char*` constructor that expects you to be handing it a pointer to a null-terminated array of characters, which `nullptr` is not. – Nathan Pierson Sep 08 '22 at 06:06
  • 2
    @MechanicPig: I suspect the Python code, if it's been run, was run with an explicitly passed `list` of `Person` objects, not the default `list` of `int` (the default being a mutable type would make it have all sorts of other potential problems to boot). – ShadowRanger Sep 08 '22 at 23:39

1 Answers1

0

The problem is that keysC is vector<int> which means keysC[0] is an expression of typeint which in turn means you can't apply operator-> to it. Similarly, keysC[values-1] is also int and so you can't apply operator-> to it either.

Jason
  • 36,170
  • 5
  • 26
  • 60