这是我的C ++课程作业。当然,我会自己完成。但是有一个我真的不知道的地方。希望有人可以向我解释。提前致谢。

此分配的要求如下所示:

我们需要使用模板来创建一个名为Set的自定义类,以实现联合(使用运算符+)和交集(使用运算符*)的功能。例如,{4,2,3}+{9,4,8,2}将为{8,3,9,2,4},而{3,2,4}*{8,4,9,2}将为{4,2}

这是xxxx.h文件的框架:

xxxx.h file

这是代码,但无法像图片一样显示红色矩形。但这更具可读性:)

#ifndef A2P2_H
#define A2P2_H
#include <exception>
#include <iostream>

using namespace std;

//========part a-Comments here:

//========part b-author's details
void info(){/* missing code */}

//========part c-exception classes:
class RemoveFromEmpty : exception{
    public:
        RemoveFromEmpty(){/* missing code */}
        const char* what() const noexcept {/* missing code */}
    private:
        string mMessage;
};

class NonExistingElem:exception{
    /* to be thrown when the element to be removed is not found in
    the set --------code missing */
};


//========part d-Set class template
template <typename EType>
class Set{
    public:
        //constructors
        Set( );
        Set( const Set & rhs );
        Set( Set && rhs );

        //destructor
        ~Set( );

        //operators overloaded
        Set & operator=( const Set & rhs );
        Set & operator=( Set && rhs );
        Set operator+( const Set & rhs ) const; //set union
        Set operator*( const Set & rhs ) const; //set intersection

        //methods
        bool isElement( const EType & x ) const;
        bool isEmpty( ) const;
        int getSize( ) const;

        //display on out all elements in the set between {..}
        void print( ostream & out = cout ) const;
        void setToEmptySet( );

        //methods to work with individual elements of a set
        void insert( const EType & x );
        void remove( const EType & x );

    private:
        struct Node{// type of the elements of the set
            EType mData;
            Node *mNext;
            Node( const EType & d = EType( ), Node *n = nullptr )
            : mData( d ), mNext( n ) { }
        };
        Node *mFirst;
        int mSize; // to have an efficient getSize().
};

//Write the definitions of all Set function members here:
//========part e-the output operator:
template <typename EType>
ostream & operator<< /* code missing here */
#endif


请参见上图,并更多注意突出显示的部分。下部有一个红色矩形,内容为const EType &d = EType()。我知道EType是我在模板中声明的类型。但是什么是EType()?是功能吗?

在需求的其余部分中,没有与EType()相关的其他内容(即该功能的某些实现)。实际上,左边是xxxx.cpp文件,是执行该代码时的结果。如果有帮助,我也将其张贴在这里。

xxxx.cpp and the result

#include <iostream>
#include "a2p2.h"
using namespace std;

//It works only for sets of integers
template <typename T = int>
void testCopyCtr(Set<T> st){
    cout<< func <<": ";
    st.insert(23); //because of this statement
    st.print();
    cout<<endl;
}

int main( ){
    info(); //authors details
    try{
        Set<int> s1, s2;
        s1.insert( 8 );
        s1.insert( 3 );
        s1.insert( 1 );
        s1.insert( 4 );
        s1.insert( 1 );
        s1.remove( 8 );
        s1.insert( 2 );
        s2.insert( 4 );
        s2.insert( 2 );
        s2.insert( 6 );
        cout << "S1: " << s1 << ", size= " << s1.getSize( ) << endl;
        cout << "S2: " << s2 << ", size= " << s2.getSize( ) << endl;
        Set<int> s3 = s1+ s2; //union
        Set<int> s4 = s1* s2; //intersection
        cout << "s1 + s2: " << s3 << endl;
        cout << "s1 * s2: " << s4 << endl;
        Set<int> s5 = s4 = s3 = s2 = s1 = s1;
        cout << "S1: " << s1 << ", size= " << s1.getSize( ) << endl;
        cout << "S2: " << s2 << ", size= " << s2.getSize( ) << endl;
        cout << "S3: " << s3 << ", size= " << s3.getSize( ) << endl;
        cout << "S4: " << s4 << ", size= " << s4.getSize( ) << endl;
        cout << "S5: " << s5 << ", size= " << s5.getSize( ) << endl;
        cout << "S4 again : " << s4 << endl;
        testCopyCtr(s4);

        Set<float> sf;
        sf.remove(3);
    }
    catch(RemoveFromEmpty ex) {
        cout<<endl<<ex.what()<<endl;
        cout<<"Nothing to be done\n";
    }
    catch(NonExistingElem ex) {
        cout<<endl<<ex.what()<<endl;
        cout<<"Nothing to be done\n";
    }

    cout<<"All is well when it ends well!\n";
    return 0;
}


不便之处,敬请原谅。

最佳答案

它是对构造函数的调用。您正在将默认构造的EType值分配给变量d作为d的默认值。

关于c++ - 在对类型名称T进行模板处理时,T()是什么意思?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46562592/

10-09 20:35