将模板放在命名空间内并使用命名空间会产生多个错误

时间:2016-11-30 15:59:04

标签: c++

我是新来的,我一直在高中的独立学习课程中工作,我遇到了名称空间模板的问题。

我正在处理的程序需要获取一组特定类型的对象(因此是模板)。该集应包含一个向量,并希望添加,删除和打印出该集的内容。

这是我的Header文件的代码

#ifndef SET_H
#define SET_H

#include "stdafx.h"
#include <vector>
using namespace std;

namespace Setthing
{
    template<class T>
    class Set
    {
    public:
        Set(vector<T>& vect);
        void addItem(vector<T>& vect, T item, bool same);
        void removeItem(vector<T>& vect, int position);
        int getSize(vector<T>& vect);
        T* toArray(vector<T>& vect);
    };
}




#endif 

这是实施文件

#include "stdafx.h"
#include "Set.h"
#include <vector>
using namespace std;

namespace Setthing
{
    template<typename T>
    Set<T>::Set(vector<T>& vect)
    {
        vect = {};
    }
    template<typename T>
    void Set<T>::addItem(vector<T>& vect, T item, bool same)
    {
        same = false;
        for (int i = 0; i < vect.size(); i++)
        {
            if (item == vect[i])
                same = true;
        }
        if (same != true)
        {
            vect.push_back(item);
        }
    }
    template<typename T>
    void Set<T>::removeItem(vector<T>& vect, int position)
    {
        vect.erase(position);
    }
    template<typename T>
    int Set<T>::getSize(vector<T>& vect)
    {
        return vect.size();
    }
    template<typename T>
    T* Set<T>::toArray(vector<T>& vect)
    {
        T arr[vect.size()];
        for (int i = 0; i < vect.size(); i++)
        {
            arr[i] = vect[i];
        }
        return arr;
    }
}

最后这里是主要功能

#include "stdafx.h"
#include <vector>
#include <string>
#include <iostream>
#include <typeinfo>
#include "Set.h"
using namespace std;
using namespace Setthing;

/*
template<class T>
class set
{
public:
    set();
    void addItem(vector<T>& vect, T item, bool same);
    void removeItem(vector<T>& vect, int position);
    int getSize(vector<T>& vect);
    T *toArray();
};
void addItem(vector<T>& vect, T item, bool same)
{
    same = false;
    for (int i = 0; i < vect.size(); i++)
    {
        if (item == vect[i])
            same = true;
    }
    if (same != true)
    {
        vect.push_back(item);
    }
}
void removeItem(vector<T>& vect, int position)
{
    vect.erase(position);
}
int getSize(vector<T>& vect)
{
    return vect.size();
}
T *toArray()
{

}*/

int wmain(int argc, wchar_t *argv[])
{
    std::vector<int> list1;
    std::vector<string> list2;

    Set set1 = Set(list1);
    Set set2 = Set(list2);

    int obj1;
    int add1;
    string obj2;
    string add2;

    int* point1;
    string* point2;
    bool same = true;

    cout << "Input an int for object 1: ";
    cin >> obj1;

    cout << "\nInput a string for object 2: ";
    cin >> obj2;

    if (typeid(obj1) == typeid(list1))
        list1.push_back(obj1);
    else
        cout << "\nERROR! The types are not the same.";

    if (typeid(obj2) == typeid(list2))
        list2.push_back(obj2);
    else
        cout << "\nERROR! The types are not the same.";

    cout << "\nEnter a value you want to add to list 1: ";
    cin >> add1;

    set1.addItem(list1, add1, same);

    cout << "\nEnter a value you want to add to list 2: ";
    cin >> add2;

    set2.addItem(list2, add2, same);

    cout << "\nRemoving the first object of each list...";

    set1.removeItem(list1, 0);
    set2.removeItem(list2, 0);

    cout << "\nPutting lists to pointers...";
    point1 = set1.toArray(list1);
    point2 = set2.toArray(list2);

    return 0;
}

我得到的错误是说Set类是遥不可及的,并且无法访问参数列表。它还说该类没有默认构造函数,并且函数无法工作。

0 个答案:

没有答案