0

I am making a program that asks the user how many robots they want to create. They then need to name those robots, and it is then stored in an array of structs.
The struct userRobot will have other values like x-y coordinated but since I'm just starting on the project I just want to start with names first.
Everything works fine until I want to print the array, which gives me the error:

error: no match for operator<< ...

#include <iostream>

using namespace std;

int main()
{
    struct userRobot{
        string name;
    };
    int NumberOfRobots;
    string robotName;
    cout << "Enter the number of robots" << endl;
    cin >> NumberOfRobots;
    cout << endl << "Enter their name(s)" << endl;
    userRobot RobotArray[NumberOfRobots];
    for(int i=0;i<NumberOfRobots;i++){
        cin >> robotName;
        RobotArray[NumberOfRobots - NumberOfRobots].name = {robotName};
        
    }
    
    for (int j = 0; j < NumberOfRobots; j++){
        cout << RobotArray[j] << endl;
    }
   
    return 0;
}
wohlstad
  • 12,661
  • 10
  • 26
  • 39
  • Dupe: [How to print the elements of an array of structures?](https://stackoverflow.com/questions/71372734/how-to-print-the-elements-of-an-array-of-structures/71372815#71372815) – Jason Sep 25 '22 at 04:32

2 Answers2

0
  1. You can't just print out the structs themselves. Trying to print out structs causes this error in your case. You can instead print out their individual members, in your case names.
for (int j = 0; j < NumberOfRobots; j++){
        cout << RobotArray[j].name << endl;
    }
  1. In the first for loop , why do you subtract NumberOfRobots from NumberOfRobots? It would always result in 0 and you r array will ultimately overwrite all of the names in the same index position. So you will never store all of the names. The correct code looks something like this
for(int i=0;i<NumberOfRobots;i++){
        cin >> robotName;
        RobotArray[i].name = robotName;

    }
Ram
  • 26
  • 3
  • 1
    "You can't just print out the structs themselves." actually you can, you just need to define `operator<<()` for it – Slava Sep 25 '22 at 02:02
  • Looks like the OP would prefer to be able to simply print the struct object as if it was a basic type. See how it can be done in my asnwer: https://stackoverflow.com/questions/73841522/cant-print-a-custom-struct-error-no-match-for-operator/73841950#73841950. – wohlstad Sep 25 '22 at 05:24
0

You can overload operator<< for ostream with your own class (userRobot). Then you'll be able to use std::cout to print your class objects as if they were basic types.

In order to do it, you need to add a function e.g.:

std::ostream& operator<<(std::ostream& os, userRobot const& ur)
{
    // print the members of userRobot: 
    os << ur.name;
    return os;
}

If your class will have private members that you would like to print, you also need to make this function a friend:

struct userRobot {
    // ...
    friend std::ostream& operator<<(std::ostream& os, userRobot const& ur);
};

Some more issues in your code:

  1. C++ standard does not support VLA (variable length arrays). See: Why aren't variable-length arrays part of the C++ standard?. You should use std::vector instead.
  2. Better to avoid using namespace std - see here Why is "using namespace std;" considered bad practice?.
  3. Since local function definitions are not allowed, it makes it simpler if struct userRobot was not inside main as well. I didn't see a good reason for it to be there anyway (assuming in a real project other modules will use this class as well).
  4. NumberOfRobots - NumberOfRobots is always 0. I think you meant to use i as the element index.

Complete version:

#include <string>
#include <iostream>
#include <vector>

struct userRobot {
    std::string name;
    friend std::ostream& operator<<(std::ostream& os, userRobot const& ur);
};

std::ostream& operator<<(std::ostream& os, userRobot const& ur)
{
    // print the members of userRobot: 
    os << ur.name;
    return os;
}

int main()
{
    int NumberOfRobots;
    std::string robotName;
    std::cout << "Enter the number of robots" << std::endl;
    std::cin >> NumberOfRobots;
    std::cout << std::endl << "Enter their name(s)" << std::endl;
    std::vector<userRobot> RobotArray(NumberOfRobots);
    for (int i = 0; i < NumberOfRobots; i++) 
    {
        std::cin >> robotName;
        RobotArray[i].name = robotName;
    }
    for (int j = 0; j < NumberOfRobots; j++)
    {
        std::cout << RobotArray[j] << std::endl;
    }
    return 0;
}
wohlstad
  • 12,661
  • 10
  • 26
  • 39