-1

i'm writing a program which requires a table, which i am emulating using a vector array, which i have made a custom class to create the table. however i cannot see any of the items in class table, except for vec_data. Why can't i access the public members in this class? for some reason, MSVC++ Intellisense can only see vec_data, nothing else.

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();
  }
}

for instance:

table<int> tblOne; //legal
table.table(int, 123); //not legal(probably not legal anyways, but intellisense says the function does not exist?)
7c00h
  • 91
  • 1
  • 10
  • The `table(const T&, unsigned int size)` is a constructor, and constructors are not named and cannot be called directly. (Destructors are officially also unnamed, but can be called directly. You just shouldn't 99.9% of the time.) – Mooing Duck Oct 16 '12 at 18:49
  • but doing this: table*tblOne = new Table(123) throws: no suitable constructor converts from int to table even though, i have a secondary param that is of type int in the constuctor – 7c00h Oct 16 '12 at 18:55
  • 1
    I rather think you have no idea what you're doing. `table(const T&)` would be a copy constructor, and `table(unsigned int)` would be a conversion constructor. What you have is a normal, two parameter constructor. To use it, you need to pass _two_ parameters. One `table`, and one `unsigned int`. – Mooing Duck Oct 16 '12 at 18:59
  • @MooingDuck: the issue of whether there are "explicit constructor calls" was once discussed up and down and sideways. even some otherwise reasonable people got into their heads that the standard's "*looks like* an explicit constructor call" indicated that most likely constructor couldn't be called, and if they could, then the calls couldn't be explicit. i used to point them towards the definition of default constructor in the standard, to deal with the nonsense about non-callability; you might want to go there too. ;-) – Cheers and hth. - Alf Oct 16 '12 at 19:06
  • 2
    This is so full of basic syntactic mistakes all I can do is ask you to stop writing code right now and [read a good book first](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – R. Martinho Fernandes Oct 16 '12 at 19:11
  • @Cheersandhth.-Alf: I had to look at the spec to understand what it is you're trying to tell me :D You are correct of course, it _is_ possible to call constructors. I merely meant to say that since constructors are unnamed, they can't be called like normal functions, and must be called with their own special syntax. – Mooing Duck Oct 16 '12 at 20:36
  • IntelliSense isn't entirely reliable, especially when code contains so many errors that it's difficult to make sense of. In other words, it behaves the way it does because you need more of [these](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Oct 16 '12 at 21:14

3 Answers3

3

If this is your real code, then you have some little mistakes:

  • In C++ each class(including struct and union) must terminated using ;, but you do not terminate tblarray with ;

  • As far as I see, in your template instantiation, T is int, but tblarray derived from T, think about it can you have struct test : int?

  • All of your properties(You do not have any function: a variable: vec_data and a type: tblarray) are private so how you want to access them?

  • In C++ this is a pointer not a reference so you must replace this. with this->

  • In order to access your specialized operator use reserved word operator so convert this.delete(vecTbl) to operator delete(vecTbl)(also this is not a good practice, operator delete is declared to delete an instance of your class not a member of it!)

  • table is constructor of your class so you should use it when you want to instantiate your variable: table<...> t(1, 100) and since you declare a non default constructor and you have no default constructor you can't have table<...> t since it require a default constructor.

BigBoss
  • 6,904
  • 2
  • 23
  • 38
0

table.table(1,123) is illegal. You can't call a constructor as such.

Lews Therin
  • 10,907
  • 4
  • 48
  • 72
  • i understand that, however, the question is not on the legality of my constructor call, but on why i can access it at all. – 7c00h Oct 16 '12 at 18:50
  • Do you mean why you can't? I guess the Intellisense figures if it is illegal to do so, there isn't any reason why you should call it like an instance member. You can call the constructor as you probably know when creating an object. – Lews Therin Oct 16 '12 at 18:52
  • "`table.table(1,123)` is invalid outside the class scope. You can call a constructor by using `::` instead of `.` there, but you need to provide the template parameter for the class template." EDIT: Well actually it's only Visual C++ that permits it. Oops. But anyway, no prohibition against constructor calls in the standard. The correct way would be `table( 1, 123 )`, assuming that constructor takes two arguments of type int. – Cheers and hth. - Alf Oct 16 '12 at 18:53
0

You've missed semicolon after struct declaration:

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();
    }
}

Also here is instantiation:

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

BTW, const T& in ctor is redundant, type already substituted in template parameter

zabulus
  • 2,373
  • 3
  • 15
  • 27