Thread: Returning a reference == int(int (*)()) ??

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,647

    Returning a reference == int(int (*)()) ??

    I have a member function:
    Code:
            T& at(int n)
            {
                return coeff.at(n);
            }
    where coeff is a std::vector<T>

    When I attempt to use the returned value in a multiplication with another T instantiated to integers, I get the following error:
    Code:
    polynomial.hpp:76:4: error: invalid operands of types ‘int(int (*)())’ and ‘int’ to binary ‘operator*’
    Here is the complete code:
    polynomial.hpp
    Code:
    #ifndef MM_POLY
    #define MM_POLY
    #include<vector>
    #include<string>
    #include<sstream>
    #include<initializer_list>
    #include<stdexcept>
    namespace mm
    {
        
        template<typename T=double>
        class Polynomial
        {
        public:
            Polynomial(std::initializer_list<T> dat)
            {
                for(auto x:dat)
                    coeff.push_back(x);
            }
            Polynomial(int n)
            {
                coeff.reserve(n+1);
            }
            std::string str(std::string var) const
            {
                std::ostringstream os;
                for(uint i=0;i<coeff.size()-1;++i)
                    os<<coeff[i]<<"*"<<var<<"^"<<i<<" + ";
                os<<coeff[coeff.size()-1]<<"*"<<var<<"^"<<coeff.size()-1;
                return os.str();
            }
            T eval(T t) const
            {
                T result = T(0);
                T mul = T(1); 
                for(int i=0;i<=degree();++i,mul*=t)
                    result+=mul*coeff[i];
                return result;
            }
            
            
            int degree() const
            {
                return coeff.size()-1;
            }
            T& at(int n)
            {
                return coeff.at(n);
            }
        private:
            std::vector<T> coeff;
        };
        
        template
        <
        typename T
        >
        std::pair
        <
        Polynomial<T>
        ,T
        >
        SyntheticDivision(Polynomial<T> dividend,Polynomial<T> divisor)
        {
            int qdeg = dividend.degree()-1;
            Polynomial<T> quotient(qdeg);
            T remainder(T());
            if(divisor.degree()!=1)
                throw(std::logic_error("SyntheticDivision:Divisor.Degree !=1"));
            
    
            T h=- divisor.at(0)/divisor.at(1);
    
            for(int i=qdeg;i>=0;--i)
            {
                remainder = dividend.at(i+1) + remainder*h;
                quotient.at(i)=remainder;
            }
            
            return std::pair<Polynomial<T>,T>(quotient,remainder);
        }
    }
    
    #endif
    Similar errors are reported at lines 76,77 and 80.
    main.cpp
    Code:
    #include "polynomial.hpp"
    #include<iostream>
    int main()
    {
        mm::Polynomial<int> dn({2,-3,1}),dv({-1,1});
        auto result = mm::SyntheticDivision<int>(dn,dv);
        std::cout<<result.first.str("x");
    }
    I couldn't produce the problem with a simpler example:
    Code:
    template<typename T>
    class Foo
    {
    public:
        Foo(T t):foo(t){};
        T& val()
        {
            return foo;
        }
    private:
        T foo;
    };
    int main()
    {
        Foo<int> bar(6);
        int xip = 5;
        int bas  = xip * bar.val();
    }
    Last edited by manasij7479; 12-16-2012 at 06:03 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>T remainder(T());
    I believe the compiler is interpreting this as a function declaration or some such.
    Changing it to
    T remainder = T();
    makes it compile for me.

    Code:
    T remainder{T()};
    Works too.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning by reference
    By cuo741 in forum C++ Programming
    Replies: 1
    Last Post: 12-09-2010, 06:06 AM
  2. returning by reference / value.
    By Kitt3n in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2010, 12:02 PM
  3. returning reference
    By l2u in forum C++ Programming
    Replies: 34
    Last Post: 04-17-2008, 11:29 AM
  4. Returning a reference? Why?
    By Mr_Miguel in forum C++ Programming
    Replies: 12
    Last Post: 09-13-2007, 11:43 AM
Website Security Test