类无法访问公共成员c ++

时间:2012-10-16 18:44:40

标签: c++ class public

我正在编写一个需要表的程序,我使用向量数组进行模拟,我已经创建了一个自定义类来创建表。但是我看不到类表中的任何项目,除了vec_data。为什么我不能访问这堂课的公众成员?出于某种原因,MSVC ++ Intellisense只能看到vec_data,没有别的。

template<class T>
class table
{
  private:
    T* vec_data;// initialize T

    struct tblarray : public T
    {
       std::vector<T> vecTbl[];
       bool operator[](unsigned int i) { return vecTbl[i]; } //redefine operator[] to accept unsigned int
       static void operator new(double n) //redefine new operator
       {
          void *d;
          if(n < 0) throw std::exception("Invalid Allocation to Negative number!");
          if(assert((d=malloc(n)) != 0) = 0) throw std::bad_alloc;
          return d;
      }
      void operator delete(void *d) //redefine delete operator
      {
        if(assert((free(p))) = 0) throw std::exception("Invalid Free of specified data!");
      }
      tblarray(const T&, unsigned int size) : T //one constructor
      {
        vecTbl = this.new std::vector<T>[reinterpret_cast<double>(size)];
      }
      ~tblarray() //one destructor
      {
        this.delete(vecTbl);
      }
}
public:
  table(const T&, unsigned int size) : T
  {
      this.tblarray.tblarray(T, size);
  }
  ~table()
  {
      this.tblarray.~tblarray();
  }
}

例如:

table<int> tblOne; //legal
table.table(int, 123); //not legal(probably not legal anyways, but intellisense says the function does not exist?)

3 个答案:

答案 0 :(得分:3)

如果这是您真正的代码,那么您有一些小错误:

  • 在C ++中,每个类(包括struct和union)必须使用;终止,但不会使用tblarray终止;

  • 据我所知,在您的模板实例化中,Tint,但tblarray来自T,请考虑一下struct test : int

  • 您的所有属性(您没有任何功能:变量:vec_data和类型:tblarray)都是私有的,所以您想如何访问它们?

  • 在C ++中,this是指针而不是引用,因此您必须将this.替换为this->

  • 要访问您的专业运营商,请使用保留字operator,以便将this.delete(vecTbl)转换为operator delete(vecTbl)(这也不是一个好习惯,operator delete已声明删除你的类的实例而不是它的成员!)

  • table是你的类的构造函数,所以当你想要实例化你的变量时你应该使用它:table<...> t(1, 100)并且因为你声明了一个非默认的构造函数而你没有默认的构造函数没有table<...> t因为它需要默认的构造函数。

答案 1 :(得分:0)

table.table(1,123)是非法的。你不能这样调用构造函数。

答案 2 :(得分:0)

结构声明后你错过了分号:

template<class T>
class table
{
private:
    T* vec_data;// initialize T

    struct tblarray
    {
        std::vector<T> vecTbl[];
        bool operator[](unsigned int i) { return vecTbl[i]; } //redefine operator[] to accept unsigned int
        static void operator new(double n) //redefine new operator
        {
            void *d;
            if(n < 0) throw std::exception("Invalid Allocation to Negative number!");
            if(assert((d=malloc(n)) != 0) = 0) throw std::bad_alloc;
            return d;
        }
        void operator delete(void *d) //redefine delete operator
        {
            if(assert((free(p))) = 0) throw std::exception("Invalid Free of specified data!");
        }
        tblarray(const T&, unsigned int size) : T //one constructor
        {
            vecTbl = this.new std::vector<T>[reinterpret_cast<double>(size)];
        }
        ~tblarray() //one destructor
        {
            this.delete(vecTbl);
        }
    };
public:
    table(const T&, unsigned int size) : T
    {
        this.tblarray.tblarray(T, size);
    }
    ~table()
    {
        this.tblarray.~tblarray();
    }
}

此处还有实例化:

int j = 1;
table<int> t(j, 2);

BTW,const T&amp;在ctor中是冗余的,类型已在模板参数

中替换