Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


some problems I need help with

This topic is 6064 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

this code is inside "decs.h"
#ifndef DECS_H
#define DECS_H

#include < windows.h >
#include < iostream >
#include < fstream >

using namespace std;

class someClass {
int x;
int y;

someClass() { } 
~someClass() { }

friend ostream operator<<(ostream &stream, someClass c);
// inserter for the class 


extern ofstream log("my.log");

#endif // DECS_H  


this code is inside "inserter.cpp":
#include "decs.h"

ostream operator<<(ostream &stream, someClass c) {
stream << "class info" << endl;

return stream;
this code is inside "main.cpp":
#include "decs.h"

int WINAPI WinMain(HINSTANCE hInstance, 
HINSTANCE hPrevInstance, 
LPSTR lpCmdLine, 
int nCmdShow) {
someClass c;

log << c;

return 0;
I have 2 problems with this code: 1. the linker throws an error saying the log variable is already defind in main.obj when linking inserter.obj why? is it because every time I include decs.h a variable is created? doesnt the extern modifier make it global to all files? 2. if I solve the first problem by moving the variable log to main.cpp (which is not a good solution since I want to use it all over the project), another problem arises: the overloaded operator doesnt have access to private members of someClass. again, Why? doesnt the friend modifier take care of that? hope it's clear enough.. btw: if anyone suggests a new subject for the topic i'd be glad to change it! Edited by - ykarj on December 13, 2001 2:32:37 PM

Share this post

Link to post
Share on other sites
a) You can''t initialize an ''extern'' variable : you''re doing a declaration, not a definition. Leave ''extern ofstream log;'' in the header and put ''ofstream log("my.log")'' in a cpp file.

b) The exact prototype for ''operator<<()'' on streams is ''ostream& operator<<(ostream&, const YourType& );''. The ''friend'' declaration should work though.

Share this post

Link to post
Share on other sites
off the top of my head, i would say the first problem could be solved by using:

extern ofstream log;

in your header and:

ofstream log( "mylog.txt" );

in only ONE of the source files.

some people think i''m crazy, some people know i am

Share this post

Link to post
Share on other sites
thanks guys, i''ll try to remove the initialization from the header file.

fruny: I have tried the operator declaration with and without the ''&''. the function just can''t access the private vars of the class for some reason.

Share this post

Link to post
Share on other sites
well, I removed the inititlization from the header file and it now works fine.

the problem with the friend function not being able to access provate members of the class still stands, so if anyone has any idea..

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!