0

Hi fellow members,

I have wrote two abstract classes, with the second inheriting from the first.

However, I receive this error when I try to compile: "error C2143: syntax error : missing ',' before '<'."

Will somebody help me out?

Thanks!

template <class DataType>
class AbstactArrayClass
{
    friend ostream& operator << (ostream& s, AbstactArrayClass<DataType>& ac);
    public:
        virtual int size() const = NULL;
        virtual DataType& operator[] (int k) = NULL;
};

template <class DataType>
ostream& operator << (ostream& s, AbstactArrayClass<DataType>& ac)
{
    s << "[";
    for (int i = 0; i < ac.size(); i++)
    {
        if (i > 0)
        {
            s << ',';
        }
        s << ac[i];
    }
    s << "]";
    return s;
}



template <class DataType>
class AbstractVector : virtual public AbstractArrayClass<DataType>
{
    public:
        virtual void insert(const DataType& item, int index) = NULL;
        virtual void remove(int index) = NULL;
        virtual void add(const DataType& item) = NULL;
};
2
Contributors
1
Reply
26
Views
2 Years
Discussion Span
Last Post by mike_2000_17
0

Well, there are a few problems with that code. I cannot, however, pinpoint where that specific error is coming from, are you sure it's within that code?

Now, for the errors that I can see:

First, you should not use = NULL for pure functions, you should use = 0. Many compilers will reject this code.

Second, you've made a spelling mistake with the "AbstractArrayClass". In the declaration of that class template, it is written as "AbstactArrayClass", and in the AbstractVector, it is written as "AbstractArrayClass". Notice the missing "r" in the first one.

Third, the friend declaration of operator<< does not match the function template definition. In fact, such friend functions are not templates at all. This is one of the more quirky and confusing parts of the C++ rules. A non-template friend function declaration for a class template declares a non-template function for each instantiation of the class template. ... I know.. this might sound like gibberish.. but read it very carefully.

For example, if you have this:

template <typename T>
struct foo {
  friend void bar(foo<T> f);
};

The bar function is not a template. What happens is that when you instantiate the foo class template for some type T, let's say you create a foo<int>, the compiler will magically make a function bar appear with the signature void bar(foo<int> f);, and that function that magically appears is not a template. So, if you declare another bar like this:

template <typename T>
void bar(foo<T> f);

The compiler will not consider them to be referring to the same thing. In other words, the void bar(foo<int> f); that magically appears when foo<int> is instantiated is just a standalone normal non-template function, and it is not considered to be a specialization of the bar function template.

Now, the next question becomes, how do you provide the definition of that magically-appearing non-template function? The problem is that even though the friend function depends on the yet-unknown type T, it is not a template. So, long story short, there is no place where you can define that function except within the class definition itself. So, the practical rule is that all non-template friends of class templates must be defined within the class template's definition. As so:

template <typename T>
struct foo {
  friend void bar(foo<T> f) {
    // .. code for bar here.
  };
};

It is preferrable to avoid friend functions if you can, and in your case, that can definitely be avoided because your implementation of the operator<< relies only on the public members of the AbstractArrayClass class template. So, you should simply remove the friend declaration, and all should be fine.

With those corrections made, your code works fine for me. I suspect your specific error occurred elsewhere, because that code you posted does not instantiate any template (or even any concrete class), which means you effectively have nothing to compile. So, please put in those corrections, try to compile again, and if you get errors, this time, you need to tell us on what line of code the error occurs.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.