简体   繁体   中英

Expected unqualified-id before token

Edit: One of the sites I've looked at that leads me to believe my syntax is correct: http://www.cprogramming.com/tutorial/templates.html

I've explored around Google a good deal before coming here to ask this question, since near as I can tell I've written things properly according to the examples that I have seen. For a homework assignment, writing a template class that does some extremely basic math stuff (sum, average, minimum, maximum, etc). When I attempt to compile to try and start debugging, I end up with the following error.

error: expected unqualified-id before '<' token

Following is somewhat snipped code, prototypes and a few lines before the chunk that errors. The code throws the error on the second line (the ones that start with lessthansymbol myType greaterthansymbol). I'm sure once I get past this I'll have plenty more things to debug with the program, but this is completely holding me up at the moment.

template <class myType>
class simpleSet
{
    public:

    static const int VALUE_MAX = 1000;
    static const int CNT_MIN = 10;
    static const int CNT_MAX = 500;

    simpleSet();
    simpleSet(int, myType[]);
    ~simpleSet();
    void gnomeSort();
    void generateNewSet(int);
    myType minimum() const;
    myType maximum() const;
    myType median() const;
    myType sum() const;
    myType average() const;
    myType linearRegression(const simpleSet&) const;
    myType getDatum(int) const;
    void setDatum(int, myType);
    int getLength() const;
    void printSet() const;
    int readCount();

private:

    int setLength;
    myType *mySet;
};

template <class myType>
void simpleSet<myType>::generateNewSet(int size)
{
    setLength = size;
    for (int i = 0; i < setLength; i++)
    {
        mySet[i] = static_cast<myType>((myType(rand()%VALUE_MAX)));
        if (mySet[i] < 1 || mySet[i] > VALUE_MAX)
        {
            i--;
            continue;
        }
    }
}

template <class myType>
<myType> simpleSet<myType>::maximum() const
{
    myType worker = mySet[0];
    for (int i = 0; i < setSize; i++)
    {
        if (worker < mySet[i]) worker = mySet[i];
    }
    return worker;
}

template <class myType>
<myType> simpleSet<myType>::minimum() const
{
    myType worker = mySet[0];
    for (int i = 0; i < setSize; i++)
    {
        if (worker > mySet[i]) worker = mySet[i];
    }
    return worker;
}

template <class myType>
<myType> simpleSet<myType>::median() const
{
    if (setSize == 1) return mySet[0];
    else if ((setSize % 2) == 1) return (mySet[(setSize/2)]);
    else return (average((mySet[setSize/2] + mySet[(setSize/2)-1])));
}

template <class myType>
<myType> simpleSet<myType>::sum() const
{
    myType temp;
for (i = 0; i < setSize; i++) temp = temp + mySet[i];
return temp;
}

template <class myType>
<myType> simpleSet<myType>::average() const
{
    return (mySet.sum()/setSize);
}

template <class myType>
<myType> simpleSet<myType>::getDatum(int item) const
{
    return(myset[item]);
}

You don't need <myType> on all the return values. Just myType (no angle brackets) Also don't need to include <myType> in the class scope (ie myType simpleSet::median() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM