Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 27th, 2006, 8:13 PM   #1
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
friend functions causing trouble

Scripting languages make programming fun, but sometimes half the fun is working creating something yourself.

I was trying to make my own String class to get my C++ memory back.
For some reason, when I try to invoke a function that I specifically made a friend, I get this error:

$ g++ myString.cpp main.cpp -o string
myString.cpp: In function ‘std::ostream& operator<<(std::ostream&, const Jesse::String&)’:
myString.h:9: error: ‘char* Jesse::String::str’ is private
myString.cpp:13: error: within this context

Which tells me that I am doing something wrong. The problem is that I have no idea what it is. Help would be appreciated.

myString.h
#include <iostream>

#ifndef MYSTRING_H_
#define MYSTRING_H_

namespace Jesse {
	class String {
		private:
			char *str;
			size_t size;
		public:
			String(const char *string);
			friend std::ostream &operator<<(std::ostream &os, const Jesse::String &str);
			size_t length() const;
			virtual ~String();
	};
}

#endif

myString.cpp
#include "myString.h"

#include <cstring>
#include <iostream>

Jesse::String::String(const char *string)
	: size(strlen(string)) {

	str = new char[size];
	strcpy(str, string);
}

std::ostream &operator<<(std::ostream &os, const Jesse::String &string) {
	os << string.str;
	return os;
}

size_t Jesse::String::length() const {
	return size;
}

Jesse::String::~String() {
	delete [] str;
}

main.cpp
#include "myString.h"

#include <iostream>

int main() {
	Jesse::String str("Hello, world!");

	std::cout << str;

	return 0;
}

Thanks
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Apr 27th, 2006, 8:28 PM   #2
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
Wrap your implementation in the Jesse namespace too. Your problem isn't the friend function, it's a namespace scoping ambiguity:
#include "myString.h"

#include <cstring>
#include <iostream>

namespace Jesse {
    String::String(const char *string)
	: size(strlen(string)) {

	str = new char[size];
	strcpy(str, string);
    }

    std::ostream &operator<<(std::ostream &os, const String &string) {
	os << string.str;
	return os;
    }

    size_t String::length() const {
	return size;
    }

    String::~String() {
	delete [] str;
    }
}
You could also qualify operator<< as Jesse::operator<<, but it's easier to see the namespace structure by extending the namespace to hold your implementation code as well.
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Apr 27th, 2006, 8:34 PM   #3
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
That fixed it. Thanks Narue!
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:07 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC