Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Encapsulate class in its own namespace

namespace

  • Please log in to reply
8 replies to this topic

#1 a.buchhorn

a.buchhorn

    CC Lurker

  • Just Joined
  • Pip
  • 5 posts

Posted 03 September 2009 - 12:46 PM

Hello,

I am to create a class in its own header file that I am to encapsulate in its own namespace. Not only do I not understand what exactly this is supposed to mean (I do know what a namespace is, though) but am also at a loss as to its implementation. I am familiar with C but am new to C++.

Here is my class:

#include <iostream>
#include <string>   //Makes the string class available
using namespace std;

class Actors {
    public:
    string actor1;
    string actor2;
    string actor3;

    public:
        Actors(string act1, string act2, string act3):  //Default constructor
            actor1(actor1), actor2(actor2), actor3(actor3) {}

        ~Actors() {} //deconstructor

    friend ostream & operator << (ostream&fred, const Actors&joe) {
        fred << joe.actor1 << joe.actor2 << joe.actor3;
        return fred;
    }
};

I also have to make the three members private but my friend operator seems to fail at that entirely. That is, were I to remove it, I would get a lot of errors in my main function.

I would appreciate any and all help,

Thank you.
  • 0

#2 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 03 September 2009 - 01:07 PM

I'm assuming this class is already in the header file.
#include <iostream>
#include <string>   //Makes the string class available
//using namespace std;
// I never employ "using namespace", especially in a header file!
namespace Acting // Just enclose everything inside of a "namespace" and you're fine
{

class Actors {
    //public:
    // Just NOT including a "public" before the strings will make all those
    // strings private. No worries! :)
    std::string actor1;
    std::string actor2;
    std::string actor3;

    public:
        Actors(std::string actor1, std::string actor2, std::string actor3):  //Default constructor
            actor1(act1), actor2(act2), actor3(act3) {}

        ~Actors() {} //destructor
    // What EXACTLY are you trying to accomplish with this? It seems
    // rather unnecessary, it looks like you're trying to concatenate a
    // string maybe, but then why would you need to use ostreams?
    // Are you trying to place a string within a stream without other
    // streams using it? You shouldn't need to use "friend" in almost
    // any circumstance, but I need to know what you're doing.
    friend ostream & operator << (ostream&fred, const Actors&joe) {
        fred << joe.actor1 << joe.actor2 << joe.actor3;
        return fred;
    }
};
}

  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#3 a.buchhorn

a.buchhorn

    CC Lurker

  • Just Joined
  • Pip
  • 5 posts

Posted 03 September 2009 - 01:17 PM

Well, what I am trying to do is simply print those strings to the screen (cout) and since I have to make those class members (actor1, actor2, actor3) private, which I have not done yet, I have to make the output stream a friend (friend is a requirement). So yes, other function are not supposed to use those members.

Thanks for your help. I have edited the code above a little bit.
  • 0

#4 a.buchhorn

a.buchhorn

    CC Lurker

  • Just Joined
  • Pip
  • 5 posts

Posted 03 September 2009 - 01:20 PM

Here is the entire program (I put everything into one .cpp file):

#include <iostream>
#include <string>   //Makes the string class available
using namespace std;

namespace Acting {

    class Actors {
        string actor1;
        string actor2;
        string actor3;

        public:
            Actors(string act1, string act2, string act3):  //Default constructor
                actor1(actor1), actor2(actor2), actor3(actor3) {}

            ~Actors() {} //deconstructor

            Actors() {}

        friend ostream & operator << (ostream&fred, const Actors&joe) {
            fred << joe.actor1 << joe.actor2 << joe.actor3;
            return fred;
        }
    };
}

int main(void) {

    Actors personality;
    personality.actor1 = "Easy to work with.\n";
    personality.actor2 = "Too demanding.\n";
    personality.actor3 = "Does not talk much.\n";

    cout << personality.actor1 << personality.actor2 << personality.actor3 << endl;

return 0;
}


  • 0

#5 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 03 September 2009 - 01:32 PM

Well.. if you just want to print them out, then just tell the machine to print them out on the standard output stream:
#include <iostream>
#include <string>   //Makes the string class available
using namespace std;

namespace Acting {

    class Actors {
        string actor1;
        string actor2;
        string actor3;

        public:
            Actors(string act1, string act2, string act3):  //Default constructor
                actor1(actor1), actor2(actor2), actor3(actor3) {}

            ~Actors() {} //deconstructor

            Actors() {}

        //friend ostream & operator << (ostream&fred, const Actors&joe) {
        //    fred << joe.actor1 << joe.actor2 << joe.actor3;
        //    return fred;
        //}
        void printOutNames () {
            using namespace std;
            cout << actor1 << " " << actor2 << " " << actor3 << endl;
        }
    };
}
That should be just fine and it won't break any encapsulation, you don't need to worry. :)
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#6 a.buchhorn

a.buchhorn

    CC Lurker

  • Just Joined
  • Pip
  • 5 posts

Posted 03 September 2009 - 01:42 PM

The assignment states that I have to print it out in the main function, though. Another requirement is that I have "define a friend operator<< that outputs the contents of an object of this type".

By the way, what is an inline function?
  • 0

#7 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 03 September 2009 - 02:06 PM

So what you basically need to do is overload the output operator for printing out values, got it.

Okay, simple, you've got everything you need there, I wasn't reading it right. O_o Anyway, that should work as long as you put "std::" in front of the two ostream objects when you're not "using namespace std". At least it does for me. What's the problem?

And an inline function is little more than a good "suggestion" to the compiler to make that function inline instead of being a standard function call. This can save clock cycles, and is incredibly useful when you have a tight loop under very fast time constraints, you can get more out of your processor that way, but programatically it has little influence.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#8 a.buchhorn

a.buchhorn

    CC Lurker

  • Just Joined
  • Pip
  • 5 posts

Posted 03 September 2009 - 02:19 PM

Well, compiling this
#include <iostream>
#include <string>   //Makes the string class available
using namespace std;

namespace Acting {

    class Actors {
    private:
        string actor1;
        string actor2;
        string actor3;

        public:
            Actors(string act1, string act2, string act3):  //Default constructor
                actor1(actor1), actor2(actor2), actor3(actor3) {}

            ~Actors() {} //deconstructor
    };
}

inline friend Acting::ostream& operator << (ostream& x, const string Actors& y) {
                x << y.actor1 << y.actor2 << y.actor3;
                return x;
            }


int main(void) {

    Actors personality;
    personality.actor1 = "Easy to work with.\n";
    personality.actor2 = "Too demanding.\n";
    personality.actor3 = "Does not talk much.\n";

    cout << personality.actor1 << personality.actor2 << personality.actor3 << endl;

return 0;
}


produces the following error messages:

|26|error: expected initializer before '&' token|

In function `int main()':|
|34|error: `Actors' was not declared in this scope|
|34|error: expected `;' before "personality"|
|35|error: `personality' was not declared in this scope|
||=== Build finished: 4 errors, 0 warnings ===|

Edited by a.buchhorn, 03 September 2009 - 02:58 PM.
updated, fixed 4 errors

  • 0

#9 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 03 September 2009 - 04:40 PM

In my opinion doing something like this is much simpler. We're not supposed to do other peoples homework for them, so I can't just GIVE you the answer, but I can help you! Here's what the main function should look like:
using namespace std;

int main(void) {
    Acting::Actors personality("Easy to Work With.\n",
                               "Too demanding.\n",
                               "Does not talk much.\n");

    cout << personality << endl;
    return 0;
}
And, as an added bonus, it works! Your biggest problems were the fact that "[noparse]inline friend Acting::ostream& (blah blah)[/noparse]" was completely malformed. You didn't need "friend" outside the class, instead you declare it friend inside the class during the class definition, then you didn't need to say "Acting::" in front of it since ostream isn't part of that namespace, third your function definition wasn't in the namespace in the first place (like it should have been), and fourth "const string Actors" doesn't work, that's two different classes, just use "const Actors" instead. :P

There was also the fact you were trying to access private strings of the Actors class inside of your main function, which is impossible (that's the point of private!). The error where it said "Actors was not declared in this scope" is because you didn't use the "Acting::" scope operation in front of Actors during the declaration. In the constructor of the Actors class, you should ask for const char* instead of std::string, since that makes it so in the constructor you can just pass string literals ("") and not build separate std::string objects. There's a few other things, but I think if you follow this you'll iron out most the problems.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?





Also tagged with one or more of these keywords: namespace

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download