0

I am a beginner in C++ and particularly data structures. This project we are working with truncated series (something like that). I get this weird compilation error and I am not sure what it is telling me. The program was running fine before I created the copy constructor so that might be the culprit. I was having a hard time making one, but I am not sure if that is how I am supposed to.

The error looks sorta like this:

/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:1077:1: note: 
      candidate template ignored: could not match
      'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>'
      against 'Series'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:1094:1: note: 
      candidate template ignored: could not match
      'shared_ptr<type-parameter-0-2>' against 'Series'
operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:1101:1: note: 
      candidate template ignored: could not match 'bitset<_Size>' against
      'Series'
operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x) 

The code:

#include <iostream>
#include <vector>

using namespace std;

class Series {
    private:
            size_t degree;
            vector <double> coefs;

    public:
            Series():degree(0){ }

            Series(size_t d): degree(d){
                    for(int i = 0; i < (degree+1); i++){
                            coefs.push_back(0.0);
                    }
            }

            Series(double term){
                    coefs[0] = term;
            }

            Series(size_t d2,vector <double> newcoeffs): Series(d2) {
                            for (int i = 1; i < (d2+1); i++) {
                                    coefs.at(i) = newcoeffs.at(i-1);
                             }
            }

            Series(const Series & rhs) {
                            degree = rhs.degree;
                            vector <double> coefs;
                            for (int i = 1; i < (degree+1); i++) {
                                    coefs.at(i) = rhs.coefs.at(i);
                            }
            }    

            ~Series() {
                    coefs.clear();
            }

            void print(ostream & out) const{
                    if (degree == 0) {
                            return;
                    }
                    for (int i = 1; i < degree+1; i++)
                            out << coefs[i] << "x^" << i << " + ";
                    out << coefs[0];
            }

};
ostream & operator <<(ostream & out, const Series & s){
                    s.print(out);
                    return out;
}

int main(){
    vector <double> v {2.1,3.5,6.2};
    vector <double> z {1.1,2.3,4.0};
    Series two(3,z);
    Series one(two);

    cout << one << end;
    cout << two << endl;
    return 0;
}
quamrana
  • 37,849
  • 12
  • 53
  • 71
  • `coefs.at(i) = rhs.coefs.at(i);` will likely lead to a segmentation fault, as your container has not been initialized before. – informaticienzero Oct 13 '17 at 12:19
  • I think you have to declare your << operator a friend operator, and define it outside the class. – Carlos A. Jimenez Holmquist Oct 13 '17 at 12:20
  • @informaticienzero I am getting this now : libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: vector Abort trap: 6, does this have anything to do with that? how do I fix it? – Suzanne Elm Oct 13 '17 at 12:47
  • @SuzanneElm Of course you get such an exception. In C++, `at` is a function accessing elements of a `std::vector` but there, your's is empty. To add values inside, you need to use `push_back`. – informaticienzero Oct 13 '17 at 12:49

2 Answers2

3

It's just a typo in your main

cout << one << end;

should be

cout << one << endl;

The unhelpful error message you got was because end is the name of a function that already exists in the std namespace. This is a good example of why using namespace std; is often advised against.

acraig5075
  • 10,588
  • 3
  • 31
  • 50
0

Not directly related to your problem, but your code will lead to a std::out_of_range exception. This is because you use at, whish is a function accessing a value inside a std::vector, not adding values in it.

for (int i = 1; i < (degree+1); i++)
    // SEGFAULT !
    coefs.at(i) = rhs.coefs.at(i);

You have to use std::vector::push_back to add elements in your container.

for (int i = 1; i < (degree+1); i++)
    coefs.push_back(rhs.coefs.at(i));

Or even shorter with modern C++ :

for (auto elem : rhs)
    coefs.push_back(elem);
informaticienzero
  • 1,796
  • 1
  • 12
  • 22