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. Need to declare a class ( defined in cpp file ) as friend

Need to declare a class ( defined in cpp file ) as friend

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
4 Posts 4 Posters 0 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.
  • R Offline
    R Offline
    Raj Abhishek
    wrote on last edited by
    #1

    Hello, I have a namespace and class defined inside a cpp file (say x.cpp ). I want to declare this class as 'friend' inside another header (say y.h), but the header won't recognize the namespace. Now, when I put the namespace in another header (x.h) and include it in y.h, I get a re-definition error ( also in x.cpp ). I can't remove the namespace definition from x.cpp, since it has been put together by a framework for me, and I won't be able to use the framework otherwise. Does this mean I can't declare this class as friend of any other class ? Regards, Raj Abhishek

    Richard Andrew x64R F S 3 Replies Last reply
    0
    • R Raj Abhishek

      Hello, I have a namespace and class defined inside a cpp file (say x.cpp ). I want to declare this class as 'friend' inside another header (say y.h), but the header won't recognize the namespace. Now, when I put the namespace in another header (x.h) and include it in y.h, I get a re-definition error ( also in x.cpp ). I can't remove the namespace definition from x.cpp, since it has been put together by a framework for me, and I won't be able to use the framework otherwise. Does this mean I can't declare this class as friend of any other class ? Regards, Raj Abhishek

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      Can you post the relevant code snippets? It's hard to tell what's going on from your description.

      The difficult we do right away... ...the impossible takes slightly longer.

      1 Reply Last reply
      0
      • R Raj Abhishek

        Hello, I have a namespace and class defined inside a cpp file (say x.cpp ). I want to declare this class as 'friend' inside another header (say y.h), but the header won't recognize the namespace. Now, when I put the namespace in another header (x.h) and include it in y.h, I get a re-definition error ( also in x.cpp ). I can't remove the namespace definition from x.cpp, since it has been put together by a framework for me, and I won't be able to use the framework otherwise. Does this mean I can't declare this class as friend of any other class ? Regards, Raj Abhishek

        F Offline
        F Offline
        Freak30
        wrote on last edited by
        #3

        If I remember correctly you can (at least in VS 6.0) include .cpp files the same way you include header files. However it's not a very good practice. You could consider defining some kind of helper class or interface class in z.h and include this file in both x.cpp and y.h. Then you could declare z a friend of y and "channel" the function calls from x through z. Alternatively you could even derive y from z and wouldn't need a friend declaration.

        The good thing about pessimism is, that you are always either right or pleasently surprised.

        1 Reply Last reply
        0
        • R Raj Abhishek

          Hello, I have a namespace and class defined inside a cpp file (say x.cpp ). I want to declare this class as 'friend' inside another header (say y.h), but the header won't recognize the namespace. Now, when I put the namespace in another header (x.h) and include it in y.h, I get a re-definition error ( also in x.cpp ). I can't remove the namespace definition from x.cpp, since it has been put together by a framework for me, and I won't be able to use the framework otherwise. Does this mean I can't declare this class as friend of any other class ? Regards, Raj Abhishek

          S Offline
          S Offline
          Stefan_Lang
          wrote on last edited by
          #4

          Have you already tried a forward declaration, like this:

          namespace myspace {
          class x; // forward declaration of class myspace::x
          };

          class y {
          friend myspace::x;
          };

          GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

          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