-
April 10th, 2022, 01:50 PM
#1
Issue with array of objects
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:stream << 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.
Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include "Friend.h"
using namespace std;
const int 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
}
Edit & Run
Friend.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//This header file contains the Friend class decleration.
#ifndef Friend_H
#define Friend_H
#include <string>
class Friend
{
private:
std::string screenName;
std::string interests;
int age;
bool used;
public:
//Default Constructor
Friend();
//Non-Default Constructor
Friend(std::string, std::string, int, bool);
//Accessors
std::string getScreenName();
std::string getInterests();
int getAge();
bool getUsed();
//Functions
void AddFriend(Friend array[], int);
void RemoveFriend(Friend array[],int);
void SearchInterest(Friend array[], int, std::string);
void DisplayFriend(Friend array[], int);
void ListFriend(Friend array[], int);
float ReportAge(Friend array[], int);
bool IsBefore(Friend one, Friend two);
void Exit();
#endif
Friend.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "Friend.h"
Friend::Friend (){
screenName = " ";
interests = " ";
age = 0;
used = false;
}
Friend::Friend(std::string inputScreenName, std::string inputInterests, int inputAge, bool determinUsed){
screenName = inputScreenName;
interests = inputInterests;
age = inputAge;
used = determinUsed;
}
std::string Friend::getScreenName(){
return screenName;
}
std::string Friend::getInterests(){
return interests;
}
int Friend::getAge(){
return age;
}
bool Friend::getUsed(){
return used;
}
void Friend::AddFriend(Friend information[], int size){
//Unused at the moment
}
Last edited on Jan 19, 2014 at 8:48am
Jan 18, 2014 at 3:56pm
MikeyBoy (5590)
At line 39 of Main.cpp you have:
cout << array[i] << endl;
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 count.
You have to provide an overloaded << operator that contains the code for doing that.
Last edited by existenceproduct; April 10th, 2022 at 04:20 PM.
-
April 11th, 2022, 01:39 AM
#2
Re: array of objects c++ newbie
What does your problem have to do with the one described by OP?
Why do you post your question in the more than eleven years old thread? Why not create a new one (and use CODE tags around all the code snippets)?
Victor Nijegorodov
-
April 11th, 2022, 04:14 AM
#3
Re: Issue with array of objects
When posting code, please use code tags so that the code is readable. Go Advanced, select the formatted code and click #
Friend is a class that stores the data for a friend. It is not a collection of friends. That is Friends array in main(). As Friend is not an in-built type, to use << to insert into a stream, you need to provide a stream insertion overload to do this. As a starter, consider as one compilable file:
Code:
#include <iostream>
#include <iomanip>
class Friend {
private:
std::string screenName;
std::string interests;
unsigned age {};
bool used {};
public:
Friend();
Friend(const std::string&, const std::string&, int, bool);
std::string getScreenName() const ;
std::string getInterests() const;
unsigned getAge() const;
bool getUsed() const;
};
std::ostream& operator<<(std::ostream& os, const Friend& f) {
return os << std::setw(20) << std::left<< f.getScreenName() << std::setw(20) << f.getInterests() << std::setw(5) << f.getAge() << " " << f.getUsed();
}
Friend::Friend() {}
Friend::Friend(const std::string& inputScreenName, const std::string& inputInterests, int inputAge, bool determinUsed) :
screenName(inputScreenName), interests(inputInterests), age(inputAge), used(determinUsed) {}
std::string Friend::getScreenName() const { return screenName; }
std::string Friend::getInterests() const { return interests; }
unsigned Friend::getAge() const {return age; }
bool Friend::getUsed() const {return used; }
constexpr size_t SIZE {5};
void printMenu();
void printArray(const Friend array[], size_t size);
int main() {
Friend friends[SIZE] {};
size_t noElms {};
friends[0] = Friend {"fooBar", "IT", 45, true};
friends[1] = Friend {"Barfoo", "CS", 54, false};
noElms += 2;
printArray(friends, noElms);
printMenu();
}
void printMenu() {
std::cout << "\n*** Network of My Friends ***\n";
std::cout << "A: Add a Friend\n";
std::cout << "R: Remove a Friend\n";
std::cout << "S: Search Interests\n";
std::cout << "D: Display All Friends\n";
std::cout << "L: List All Friends in Alphabetic Order Base on Screen Name\n";
std::cout << "C: Calculate the Average Age of All Friends in My Newtork\n";
std::cout << "E: Exit\n";
std::cout << "Selection: ";
}
void printArray(const Friend array[], size_t size) {
for (size_t i {}; i < size; ++i)
std::cout << array[i] << '\n';
}
Last edited by 2kaud; April 11th, 2022 at 04:16 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|