In my main.cpp function, I'm trying to print out an array of objects which I called "Friends." When I try to do that with my printArray function I get an error that states, "Error: no operator "<<" matches these operands operand types are: std::ostream << Friend." I don't understand what that error is saying, but my best guess would be I'm using an object and my compiler doesn't know how to interact with it.
If anyone could clarify what this is saying I would appreciate it. Any other suggestions are welcome. Any answer is welcome of course, but keep in mind I'm still a novice.
**Please Read Below for updated Question! Thanks for the other reply's.
#include <iostream>
#include "Friend.h"
usingnamespace std;
constint SIZE = 5;
void printMenu();
void printArray(Friend array[], int size);
int main()
{
//Creating an array of objects of the type Friend
Friend Friends[SIZE];
//Passing the newly created array to the print array function
//EDIT
Friends.AddFriend(Friends, SIZE); // This is how I try and call my function.
printArray(Friends, SIZE);
printMenu();
return 0;
}
void printMenu(){
cout << "*** Network of My Friends ***" << endl;
cout << "A: Add a Friend" << endl;
cout << "R: Remove a Friend" << endl;
cout << "S: Search Interests" << endl;
cout << "D: Display All Friends" << endl;
cout << "L: List All Friends in Alphabetic Order Base on Screen Name" << endl;
cout << "C: Calculate the Average Age of All Friends in My Newtork" << endl;
cout << "E: Exit" << endl;
cout << "Selection:";
}
void printArray(Friend array[], int size){
for (int i = 0; i < SIZE; i++)
cout << array[i] << endl;
//Getting error message after the cout statement
}
array[i] is of type Friend. So you're trying to use the << operator, with one of the operands being of type Friend.
The problem is that the compiler doesn't know about any << that works with Friend. It can't possibly already have an operator defined for that type, because Friend is a class that you've written. It can't magically know how to stream the contents of a Friend class to cout.
You have to provide an overloaded << operator that contains the code for doing that.
You can't print an object like that. The computer doesn't know what to print in that object. You can make a function like getDetails() to get the details and print that. There is something called overloading operators but that is more advanced. Use something like below:
How would I call the AddFriend() in my main.cpp file? When I try the following it doesn't work.
Friends.AddFriend(Friends, SIZE);
The error it gives me is, "The Expression must have a class type." When I put Friends (name of the class), in front of the statement above, it doesn't work again. Also isn't like I'm creating another object of type friends, not using the existing object I made earlier?
Friend Friends.AddFriend(Friends, SIZE); (Doesn't Work Either)
Friends isn't an object of a class. It's an array of objects. It doesn't have any methods, although the elements stored in the array do. If you want to call a method on one of the elements of the array, you can, by specifying which array element you want to use just like you would with any array, like so:
1 2
// This calls the AddFriend method of the first element in the array
Friends[0].AddFriend(Friends, SIZE);
But the design of this looks weird to me. What is the AddFriends method supposed to do? Add the Friend object to the array? Because if so, doing the above makes no sense, because you're calling it on an object that's already in the array!
Presumably, what you're really trying to do is:
1) Create a Friend object
2) Add it to a container (in this case, an array) called Friends
void Friend::AddFriend(Friend information[], int size){
int count = 0;
bool singleEntry = false;
//Count the number used objects
for(int i = 0; i < size; i++){
if(information[i].used == 0 && singleEntry == false){
//0 would mean used is false
std::cout << "*** Add a new friend profile" << std::endl;
std::cout << "Screen Name: ";
std::cin.ignore();
std::getline(std::cin, information[i].screenName);
std::cout << "Interests: ";
std::getline(std::cin, information[i].interests);
//std::cin.ignore();//Clear the input buffer
std::cout << "Age: ";
std::cin >> information[i].age;
std::cout << std::endl;
information[i].used = true;
singleEntry = true;
}
else{
count++;
if (count == 5){
std::cout << "No empty space. Please delete a profile if you want to add another.";
}
}
}
}
After running the function I would want Friends[0] to contain a screen name stored as a string, interests stored as a string, and age stored as an int. Also, each element contains a bool that states if it is full or not.
I would like to note, that I completed this program. Only after its completion did I realize my instructor wanted me to write a struct, not a class. haha.
I don't know if that answered what you were asking. I hope it did.