Jump to content

using constructor in templates

- - - - -

  • Please log in to reply
2 replies to this topic

#1
notes

notes

    Learning Programmer

  • Members
  • PipPipPip
  • 48 posts
Hi,

While making own double ended queue class on templates I've noticed something weird, I've already found a solution but don't know how it is so,
look at code :

#include <cstdlib>

#include <iostream>

#include <deque>


using namespace std;


int main(int argc, char *argv[])

{


    deque<float> c ();

    //c.push_back(1.0);

   

    system("PAUSE");

    return EXIT_SUCCESS;

}

The code above compiles without any problems ( using Dev c++ 4.9.9.2 + windows 7/vista -tested on both).
Next thing we have is commented c.push_back(1.0); when I remove comment I get error :

16 G:\JP III\5.12.2011\w domu\main.cpp request for member `push_back' in `c', which is of non-class type `std::deque<float, std::allocator<float> > ()()' 

 G:\JP III\5.12.2011\w domu\Makefile.win [Build Error]  [main.o] Error 1 

Everything works fine when i delete brackets () in declaration.
I know that this may be caused because there are many constructors for deque library, but while making my project i have the same error when i have declared only one default constructor. Any one knows why I have to use: myQueue<type> q; instead of: myQueue<type> q(); ?
Remebre about KISS & DRY

#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
If you're using ctors that take no arguments then you mustn't write () because then compiler thinks it's a function. More info here.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
notes

notes

    Learning Programmer

  • Members
  • PipPipPip
  • 48 posts
Thanks for fast answer.
Remebre about KISS & DRY




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users