Template object without object name

This is a discussion on Template object without object name within the C++ Programming forums, part of the General Programming Boards category; hi The below code compiles Code: typedef Correlation<vsip::Vector,support_full,value_type> cor; Domain<1> aa(nrange); cor co(aa,aa); But if i modify the same code ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    32

    Template object without object name

    hi
    The below code compiles
    Code:
    typedef Correlation<vsip::Vector,support_full,value_type> cor;
    Domain<1> aa(nrange);
    cor co(aa,aa);
    But if i modify the same code like below i get error
    Code:
    typedef Correlation<vsip::Vector,support_full,value_type> cor;
    cor co(Domain<1>(nrange),Domain<1>(nrange));
    why the object is not created with out the object name.
    The error is
    Code:
    ../ds.cpp:86: error: conversion from `vsip::Vector<std::complex<float>, vsip::Dense<1u, std::complex<float>, vsip::tuple<0u, 1u, 2u>, vsip::Local_map> >' to non-scalar type `vsip::Domain<1u>' requested
    ../ds.cpp:127:5: warning: no newline at end of file
    make: *** [ds.o] Error 1

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Again the function thing. I don't know why the compiler gives you this particular error, but "co" is once again a function returning a "cor" and taking two arguments of type "Domain<1>". You may get problems since the arguments have the same name.

    Also, would you please just add the final newline to the file to silence this compiler warning?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    32
    Thanks for reply
    i for got to tell u that nrange is defined as
    Code:
    long unsigned int nrange = 4;
    Even if nrange is an unsigned int object, does the compiler sees co as a function.

    Also with the same compiler in the below code ,the object of Convolution is created.The second argument to the constructor is an object(which is created with out object name).

    Code:
    typedef Convolution<vsip::Vector,nonsym,support_full,value_type> con;
    con c(replica, Domain<1>(nrange));
    I am not sure whether an object can be created without object name.
    The following example is taken from the text book which creates object without using object name

    Code:
    #include <iostream>
    using namespace std;
    
    template<class T, class U> class C
    {
    public:
    	void f()
    	{
    		cout << "Primary Template\n";
    	}
    };
    
    int main()
    {
    	C<float, int>().f(); 
    }
    In the above examle does the statement
    Code:
    C<float, int>().f();
    creates a object and then calls the function f() or does somthing else.
    Please confirm wheather a object can be created without using the object name.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Even if nrange is an unsigned int object, does the compiler sees co as a function.
    The compiler doesn't care about the current meaning of nrange.

    Also with the same compiler in the below code ,the object of Convolution is created.
    Yes. Since replica isn't a type, the line can't be a function declaration, so it must be an object creation.

    I am not sure whether an object can be created without object name.
    It can be. The object is temporary, though - it is destroyed at the end of the statement.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    32
    Thank u
    If a constructor takes a pointer as an argument then new operator can be passed as the argument( new ClassName ) to create a object with out creating object name.

    In the same way if a constructor takes an object by value or reference as an argument, is there a way to create a object without creating object name.
    Last edited by babu198649; 09-16-2008 at 06:30 AM.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    By value or by const reference. Mutable references cannot bind to temporaries. (Except as an extension in VC++.)

    By the way, please drop the netspeak.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    32
    Please post a simple working code,i tried to do it but i am unable to make work

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Working code for what?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Jul 2008
    Posts
    32
    By value or by const reference.
    i am unable to make it work by value or by const reference.

    Mutable references cannot bind to temporaries. (Except as an extension in VC++.)
    As i know mutable variables can be altered by const functions,Is this what Mutable in the above statement mean or something else(I am not VC++ programmer).

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Code:
    void foo1(std::string); // by value
    void foo2(std::string &); // by mutable reference
    void foo3(const std::string &); // by const reference
    
    int main()
    {
      foo1(std::string("hey")); // Valid, temporary for by-value
      foo2(std::string("error")); // Invalid, temporary for mutable reference
      foo3(std::string("there")); // Valid, temporary by const reference
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Need help with this linker error... please
    By radroach in forum C++ Programming
    Replies: 4
    Last Post: 06-24-2005, 09:11 AM
  5. Template problems
    By VirtualAce in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2002, 11:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21