Ostream&

Editing code for class and i get the error:

basic_demo.cpp: In function 'void print(std::string*, int, std::ostream&)':
basic_demo.cpp:72:54: warning: default argument given for parameter 3 of 'void print(std::string*, int, std::ostream&)' [-fpermissive]
set.h:12:6: warning: after previous specification in 'void print(std::string*, int, std::ostream&)' [-fpermissive]

when i try to compile.

1
2
3
4
5
6
7
8
9
void print(std::string arr[], int n, ostream &os=cout){
 73 
 74        os << "{";
 75        int i = 0;
 76        os << arr [i] << ", ";
 77        os << arr[i+1];
 78        os << "}";
 79 
 80 }


thats my print function and here's my set.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
1 #ifndef SET_H
  2 #define SET_H
  3 
  4 #include <iostream>
  5 #include <string>
  6 
  7 const int
  8         CAPACITY_EXCEEDED = 1;
  9 
 10 bool contains(std::string arr[], int n, std::string val);
 11 bool add(std::string arr[], int &n, int maxN, std::string val);
 12 void print(std::string arr[], int n, std::ostream &os=std::cout);
 13 
 14 #endif 


Any help or insight would be appreciated.

EDIT: GOT HELP WITH IT, WILL POST IF AGAIN IF I GET STUMPED.
Last edited on
That isn't an error, it's a warning.

-fpermissive is typically used by gcc when moving C code to C++. It relaxes C++'s strict pointer rules and allows C's relatively relaxed pointer conversion rules.

You can't specify a default parameter in both the prototype and the implementation. Remove the default specifier from the function.
thanks btw. think you can help with my add function? Its purpose to add elements to the array unless the element is already in the array.

1
2
3
4
5
6
7
8
9
 bool add(std::string arr[], int &n, int maxN, std::string val){
 62  
 63         if (n < maxN){
 64                 arr [n] = val;
 65                 n++;
 66                 return true;}
 67         else    
 68                 return false;
 69  }              


my output is:
1
2
3
4
5
6
7
After initial creation of set: {}
Result of adding dog: true   {dog}
Result of adding cat: true   {dogcat}
Result of adding dog: true   {dogcatdog}

dog in set: true
cow in set: false


but the output is supposed to be:
1
2
3
4
5
6
7
After initial creation of set: {}
Result of adding dog: true   {dog}
Result of adding cat: true   {dog, cat}
Result of adding dog: false   {dog, cat}

dog in set: true
cow in set: false
Well, the problem is that your function always adds the val to the array unless there is no space. You should check to see if the value is in the array before adding it to the end.
I've tried:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
26 bool add(std::string arr[], int &n, int maxN, std::string val){
 27 
 28 
 29 
 30         for (int i=0; i < n; i++){
 31                 if (val==arr[i])
 32                         return false;
 33                 else
 34                         if (n <= maxN){
 35                                 arr[n] = val;
 36                                 n++;
 37                                 return true;}
 38 }
 39 
 40 


but it yields:

1
2
3
4
5
6
7
After initial creation of set: {}
Result of adding dog: false   {}
Result of adding cat: false   {}
Result of adding dog: false   {}

dog in set: false
cow in set: false
Topic archived. No new replies allowed.