Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. illegal use of this type as an expression

illegal use of this type as an expression

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++question
10 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hsuch
    wrote on last edited by
    #1

    Hi all, I'm new to C++. I've a stupid problem. This is the code I used: class A { int x; }; class B { template<class T> void temp(void){ }; void test(void); }; void B::test(void) { temp<A>(); }; Why can't I compile this code? The error message is: test.cpp(23) :error C2275: 'A' : illegal use of this type as an expression test.cpp(23) : error C2059: syntax error : ')' If I move the template function temp() out from class B, then it is ok. Could someone help me? Thanks a lot.

    S C D M 4 Replies Last reply
    0
    • H hsuch

      Hi all, I'm new to C++. I've a stupid problem. This is the code I used: class A { int x; }; class B { template<class T> void temp(void){ }; void test(void); }; void B::test(void) { temp<A>(); }; Why can't I compile this code? The error message is: test.cpp(23) :error C2275: 'A' : illegal use of this type as an expression test.cpp(23) : error C2059: syntax error : ')' If I move the template function temp() out from class B, then it is ok. Could someone help me? Thanks a lot.

      S Offline
      S Offline
      Sceptic Mole
      wrote on last edited by
      #2

      Which compiler do you use?

      H 1 Reply Last reply
      0
      • H hsuch

        Hi all, I'm new to C++. I've a stupid problem. This is the code I used: class A { int x; }; class B { template<class T> void temp(void){ }; void test(void); }; void B::test(void) { temp<A>(); }; Why can't I compile this code? The error message is: test.cpp(23) :error C2275: 'A' : illegal use of this type as an expression test.cpp(23) : error C2059: syntax error : ')' If I move the template function temp() out from class B, then it is ok. Could someone help me? Thanks a lot.

        C Offline
        C Offline
        Chris Losinger
        wrote on last edited by
        #3

        it works fine in VS 2005, but fails in VC6. not sure why it fails.

        image processing toolkits | batch image processing | blogging

        M 1 Reply Last reply
        0
        • H hsuch

          Hi all, I'm new to C++. I've a stupid problem. This is the code I used: class A { int x; }; class B { template<class T> void temp(void){ }; void test(void); }; void B::test(void) { temp<A>(); }; Why can't I compile this code? The error message is: test.cpp(23) :error C2275: 'A' : illegal use of this type as an expression test.cpp(23) : error C2059: syntax error : ')' If I move the template function temp() out from class B, then it is ok. Could someone help me? Thanks a lot.

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Are you wanting something like:

          class A
          {
          int x;
          };

          class B
          {
          template<class T> void temp(const T& data){ }
          void test(void);
          };

          void B::test(void)
          {
          A a;
          temp(&a);
          };


          "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

          "Judge not by the eye but by the heart." - Native American Proverb

          1 Reply Last reply
          0
          • C Chris Losinger

            it works fine in VS 2005, but fails in VC6. not sure why it fails.

            image processing toolkits | batch image processing | blogging

            M Offline
            M Offline
            Michael Dunn
            wrote on last edited by
            #5

            VC6 has problems when a template type parameter doesn't appear in the function's parameter list, as is the case in hsuch's sample code. The usual workaround is to add a dummy T* param that defauls to NULL.

            --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?

            C 1 Reply Last reply
            0
            • H hsuch

              Hi all, I'm new to C++. I've a stupid problem. This is the code I used: class A { int x; }; class B { template<class T> void temp(void){ }; void test(void); }; void B::test(void) { temp<A>(); }; Why can't I compile this code? The error message is: test.cpp(23) :error C2275: 'A' : illegal use of this type as an expression test.cpp(23) : error C2059: syntax error : ')' If I move the template function temp() out from class B, then it is ok. Could someone help me? Thanks a lot.

              M Offline
              M Offline
              Michael Dunn
              wrote on last edited by
              #6

              If you're using VC6, change it to

              template<class T> void temp(T* = NULL) { }

              to work around a compiler bug.

              --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?

              H 1 Reply Last reply
              0
              • M Michael Dunn

                VC6 has problems when a template type parameter doesn't appear in the function's parameter list, as is the case in hsuch's sample code. The usual workaround is to add a dummy T* param that defauls to NULL.

                --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?

                C Offline
                C Offline
                Chris Losinger
                wrote on last edited by
                #7

                you mean like this?

                class A
                {
                int x;
                };

                class B
                {
                template < class T > void temp(T *)
                {
                }
                void test(void);
                };

                void B::test(void)
                {
                A z;
                temp< A >(&z);
                };

                that doesn't work either. edit: adding the "T* = NULL" works, though

                image processing toolkits | batch image processing | blogging

                M 1 Reply Last reply
                0
                • C Chris Losinger

                  you mean like this?

                  class A
                  {
                  int x;
                  };

                  class B
                  {
                  template < class T > void temp(T *)
                  {
                  }
                  void test(void);
                  };

                  void B::test(void)
                  {
                  A z;
                  temp< A >(&z);
                  };

                  that doesn't work either. edit: adding the "T* = NULL" works, though

                  image processing toolkits | batch image processing | blogging

                  M Offline
                  M Offline
                  Michael Dunn
                  wrote on last edited by
                  #8

                  See my response below.

                  --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?

                  1 Reply Last reply
                  0
                  • S Sceptic Mole

                    Which compiler do you use?

                    H Offline
                    H Offline
                    hsuch
                    wrote on last edited by
                    #9

                    Sorry for missing the platform. I use VC6.0. I also try on VC Express, it is ok. So, it is compiler problem?

                    1 Reply Last reply
                    0
                    • M Michael Dunn

                      If you're using VC6, change it to

                      template<class T> void temp(T* = NULL) { }

                      to work around a compiler bug.

                      --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?

                      H Offline
                      H Offline
                      hsuch
                      wrote on last edited by
                      #10
                      class A
                      {	
                      	int x;
                      };
                      class B
                      {	
                      	template<class T> void temp(T* = NULL){}	
                      	void test(void);
                      };
                      void B::test(void)
                      { 	
                      	temp<A>();  
                      };
                      

                      Still doesn't work. :(

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups