Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Expected primary expression before * token

+3 votes
asked Jun 3, 2021 by Natalia (1,080 points)
Hello! In the code I'm writing there is a vector of objects of class Bus and a vector of objects of class Driver. I need to establish some connection (aggregation) between buses and objects so that one bus can belong to one and more drivers. I made a template for this purpose, but the only thing I get from the compiler is the sentence I wrote in the title.
I'm giving you the link to the code as the code is quite long.
https://onlinegdb.com/VB-xSSe6B

1 Answer

+1 vote
answered Jun 3, 2021 by Peter Minarik (84,720 points)
selected Jun 5, 2021 by Natalia
 
Best answer

Getting Rid of the Error Message

Kindly replace line

main.cpp(505): buses[0].set_driver(Driver* &drivers[0],Driver* &drivers[1]);

with

main.cpp(505): buses[0].set_driver(&drivers[0],&drivers[1]);

Hello Ikarus! :)

So I looked into your data file and the manufacturer of your first bus is Ikarus from Hungary. First time I saw anyone from outside of Hungary knowing about these busses. Heart warming. :)

I'm Confused

After you get rid of the error message as suggested above your code still wouldn't compile. It's still line 505, the compiler cannot find a matching set_driver function.

So it got me thinking... Your code involves some "crazy" template to add multiple drivers to the bus (crazy for me, as I don't really work with C++, so these things do not come naturally to me). You also use const for your constant functions. So these are not a sign of someone who just recently started to do C++.

On the other hand, you do not get your indentations right all the time, you do not use constructor to set the values of your classes (, you can have inherited Driver from Staff), you only log if a file cannot be opened, regardless, in these cases you go on with trying to read from them and closing the file (which doesn't seem to cause any problem apparently, but totally unnecessary). Misleading names (such as set_driver is used which does not set the driver, rather adds the driver to the vector of drivers, so add_driver would have been a proper name; or arrays typically suggest multiple: instead of driver, you should call your vector<Driver*> in Vehicle class as drivers.) And of course, you cannot see just by looking at line 505 what "Driver *" makes no sense there are all (it's not a cast, as missing the parenthesis, nor is driver any known variable so the * operator couldn't be applied).

These are a signs of someone who is new or has not much experience in C++.

It's like the code is written by multiple people with various level of understanding of C++.

Full Fix

Please, see the fix below so your code can run. The fix is to add drivers one-by-one to the busses. If you do want to keep the "template magic" feel free to experiment with it or do some research on your own; I am not familiar with them so I did the "old-school" implementation of adding elements one at a time. (One could create a function overload where a vector<Driver*> is taken as an argument, if adding multiple drivers is indeed needed.)

Legend:

  • delete lines
  • added lines
void set_driver() {}
template<typename T, typename... Args>
void set_driver(Driver* driver, Args... args) {
driver_.push_back(driver);
set_driver(args...);
}
void add_driver(Driver* driver) {
    driver_.push_back(driver);
}
buses[0].set_driver(Driver* &drivers[0],Driver* &drivers[1]);
buses[0].add_driver(&drivers[0]);
commented Jun 4, 2021 by Natalia (1,080 points)
Thank you Peter! I've edited my code a bit. Now it compiles. I tried to add three drivers to each bus using a cycle. However, I doubt this cycle is correct, because it works only for the first few buses. Here's the link https://onlinegdb.com/nsHpcEyj6
asked Jun 5, 2021 by Natalia (1,080 points) I'm afraid this cycle isn't perfectly correct
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...