指针作为成员函数错误

时间:2014-09-15 04:08:37

标签: c++

我正在尝试创建以下成员函数,但我收到以下错误:

class ListStudentGradeDB
{

private:


public:
    struct node 
    {
        std::string studentName;
        int studentScore;
        node *next;

    };
    // line below is to create shortcut to node* nodePtr.
    typedef struct node* nodePtr;

    nodePtr head;
    nodePtr curr;
    nodePtr temp;


    // Constructor
    ListStudentGradeDB();


    // member functions for merge sort
    void MergeSort();
    nodePtr msort(nodePtr start, int size);
    nodePtr merge(nodePtr list1, nodePtr list2, int size1, int size2);

    // Destructor
    ~ListStudentGradeDB(void);


};



    //ListStudentGradeDB.cpp 

    nodePtr ListStudentGradeDB::msort(node* start, int size)
    {
        if(size > 1)
        {
            int midSize = size/2;
            int count = midSize;
            node* mid = start;
            while(count)
        {
            mid = mid->next;
            count--; 
        }
        return merge(msort(start, midSize), msort(mid, size - midSize), midSize, size - midSize);
    }
    else
        return start;
}   


nodePtr ListStudentGradeDB::merge(nodePtr list1, nodePtr list2, int size1, int size2)
{
    // Trivial cases
    if(size1 == 0)
        return list2;

    if(size2 == 0)
        return list1;

    // Choose the bigger element from the front of the two lists
    // and put it at the head of the new list and call merge 
    // again with the sub lists

    if (list1->studentScore < list2->studentScore || list1->studentScore == list2->studentScore)
    {
        list1->next = merge(list1->next, list2, size1-1, size2);
        return list1;
    }
    else
    {
        list2->next = merge(list1, list2->next, size1, size2-1);
        return list2;
    }
}

错误

> 1>c:\users\vypham\documents\visual studio
> 2012\projects\cmpe_180\cmp180_final\liststudentgradedb.cpp(534): error
> C2143: syntax error : missing ';' before 'ListStudentGradeDB::msort'
> 1>c:\users\vypham\documents\visual studio
> 2012\projects\cmpe_180\cmp180_final\liststudentgradedb.cpp(534): error
> C4430: missing type specifier - int assumed. Note: C++ does not
> support default-int 1>c:\users\vypham\documents\visual studio
> 2012\projects\cmpe_180\cmp180_final\liststudentgradedb.cpp(535): error
> C4430: missing type specifier - int assumed. Note: C++ does not
> support default-int 1>c:\users\vypham\documents\visual studio
> 2012\projects\cmpe_180\cmp180_final\liststudentgradedb.cpp(535): error
> C2556: 'int ListStudentGradeDB::msort(ListStudentGradeDB::node *,int)'
> : overloaded function differs only by return type from
> 'ListStudentGradeDB::nodePtr
> ListStudentGradeDB::msort(ListStudentGradeDB::nodePtr,int)' 1>        
> c:\users\vypham\documents\visual studio
> 2012\projects\cmpe_180\cmp180_final\liststudentgradedb.h(88) : see
> declaration of 'ListStudentGradeDB::msort'
> 1>c:\users\vypham\documents\visual studio
> 2012\projects\cmpe_180\cmp180_final\liststudentgradedb.cpp(535): error
> C2040: 'ListStudentGradeDB::msort' : 'int (ListStudentGradeDB::node
> *,int)' differs in levels of indirection from 'ListStudentGradeDB::nodePtr (ListStudentGradeDB::nodePtr,int)'
> 1>c:\users\vypham\documents\visual studio
> 2012\projects\cmpe_180\cmp180_final\liststudentgradedb.cpp(546): error
> C2264: 'ListStudentGradeDB::msort' : error in function definition or
> declaration; function not called 1>c:\users\vypham\documents\visual
> studio
> 2012\projects\cmpe_180\cmp180_final\liststudentgradedb.cpp(553): error
> C2143: syntax error : missing ';' before 'ListStudentGradeDB::merge'
> 1>c:\users\vypham\documents\visual studio
> 2012\projects\cmpe_180\cmp180_final\liststudentgradedb.cpp(553): error
> C4430: missing type specifier - int assumed. Note: C++ does not
> support default-int 1>c:\users\vypham\documents\visual studio
> 2012\projects\cmpe_180\cmp180_final\liststudentgradedb.cpp(553): error
> C2086: 'int nodePtr' : redefinition 1>         
> c:\users\vypham\documents\visual studio
> 2012\projects\cmpe_180\cmp180_final\liststudentgradedb.cpp(534) : see
> declaration of 'nodePtr' 1>c:\users\vypham\documents\visual studio
> 2012\projects\cmpe_180\cmp180_final\liststudentgradedb.cpp(554): error
> C4430: missing type specifier - int assumed. Note: C++ does not
> support default-int 1>c:\users\vypham\documents\visual studio
> 2012\projects\cmpe_180\cmp180_final\liststudentgradedb.cpp(554): error
> C2556: 'int
> ListStudentGradeDB::merge(ListStudentGradeDB::nodePtr,ListStudentGradeDB::nodePtr,int,int)'
> : overloaded function differs only by return type from
> 'ListStudentGradeDB::nodePtr
> ListStudentGradeDB::merge(ListStudentGradeDB::nodePtr,ListStudentGradeDB::nodePtr,int,int)'
> 1>          c:\users\vypham\documents\visual studio
> 2012\projects\cmpe_180\cmp180_final\liststudentgradedb.h(89) : see
> declaration of 'ListStudentGradeDB::merge'
> 1>c:\users\vypham\documents\visual studio
> 2012\projects\cmpe_180\cmp180_final\liststudentgradedb.cpp(554): error
> C2040: 'ListStudentGradeDB::merge' : 'int
> (ListStudentGradeDB::nodePtr,ListStudentGradeDB::nodePtr,int,int)'
> differs in levels of indirection from 'ListStudentGradeDB::nodePtr
> (ListStudentGradeDB::nodePtr,ListStudentGradeDB::nodePtr,int,int)'
> 1>c:\users\vypham\documents\visual studio
> 2012\projects\cmpe_180\cmp180_final\liststudentgradedb.cpp(568): error
> C2264: 'ListStudentGradeDB::merge' : error in function definition or
> declaration; function not called 1>c:\users\vypham\documents\visual
> studio
> 2012\projects\cmpe_180\cmp180_final\liststudentgradedb.cpp(573): error
> C2264: 'ListStudentGradeDB::merge'

我正在尝试为两个成员函数返回指针。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

nodePtr具有类范围,因此您需要在ListStudentGradeDB.cpp的定义中编写ListStudentGradeDB :: nodePtr。

答案 1 :(得分:0)

您的问题似乎很可能与您的功能的实现有关:

nodePtr ListStudentGradeDB::merge( ...

nodePtrListStudentGradeDB中定义,就像merge一样。因此,您需要确保按如下方式访问该范围:

ListStudentGradeDB::nodePtr ListStudentGradeDB::merge( ...