2 Replies - 2058 Views - Last Post: 23 August 2009 - 09:00 PM Rate Topic: -----

#1 BlackPhoenix  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 152
  • Joined: 11-July 09

Accessing class object in another .cpp file

Posted 23 August 2009 - 06:05 PM

Hi,

I have a program I'm working on that is currently split up into multiple .cpp and their respective header files, and a main.cpp.

In main.cpp, I include a header for a Timer class, and from within main(), I create Timer objects

Timer regulate;
Timer update;
Timer fps;



From within main.cpp, I can have each Timer object call its Class' functions.

Example:
fps.start();
fps.stop();



What I have done is try to have main() run the main loop, and use OOP to split up all of the different features of the program.

I am trying to work with a piece of code, but I don't want it in main. I want it to be taken care of by a function in another file, Game.cpp. void Game::Update()

if(regulate.get_ticks() < 1000/FRAMES_PER_SECOND)
	{
		SDL_Delay((1000/FRAMES_PER_SECOND) - regulate.get_ticks());
	}

	if (update.get_ticks() > 1000)
	{
		std::stringstream calculatedFPS;
		calculatedFPS << "Avg FPS: " << frame/(fps.get_ticks() / 1000.f);
		displayFPS = TTF_RenderText_Solid(font, calculatedFPS.str().c_str(), textColor);
		update.start();
	}



I'm obviously getting errors because Game.cpp does not know about the existence of the Timer objects that I have created from within main.cpp.

How do I let a function in Game.cpp work with these objects from the Timer.cpp class created from within Main.cpp?



The Errors:

1>c:\users\jeremy\documents\programming\sdltutorials\sdltutorials\game.cpp(130) : error C2065: 'regulate' : undeclared identifier
1>c:\users\jeremy\documents\programming\sdltutorials\sdltutorials\game.cpp(130) : error C2228: left of '.get_ticks' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\jeremy\documents\programming\sdltutorials\sdltutorials\game.cpp(132) : error C2065: 'regulate' : undeclared identifier
1>c:\users\jeremy\documents\programming\sdltutorials\sdltutorials\game.cpp(132) : error C2228: left of '.get_ticks' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\jeremy\documents\programming\sdltutorials\sdltutorials\game.cpp(135) : error C2065: 'update' : undeclared identifier
1>c:\users\jeremy\documents\programming\sdltutorials\sdltutorials\game.cpp(135) : error C2228: left of '.get_ticks' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\jeremy\documents\programming\sdltutorials\sdltutorials\game.cpp(138) : error C2065: 'fps' : undeclared identifier
1>c:\users\jeremy\documents\programming\sdltutorials\sdltutorials\game.cpp(138) : error C2228: left of '.get_ticks' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\jeremy\documents\programming\sdltutorials\sdltutorials\game.cpp(140) : error C2065: 'update' : undeclared identifier
1>c:\users\jeremy\documents\programming\sdltutorials\sdltutorials\game.cpp(140) : error C2228: left of '.start' must have class/struct/union
1> type is ''unknown-type''


Is This A Good Question/Topic? 0
  • +

Replies To: Accessing class object in another .cpp file

#2 BlackPhoenix  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 152
  • Joined: 11-July 09

Re: Accessing class object in another .cpp file

Posted 23 August 2009 - 06:42 PM

If it helps you help me, I changed the function definition of the function in Game.cpp

void Game::Update(Timer* fps, Timer* update, Timer* regulate)



and from within Main.cpp, I pass by reference the 3 objects I want to work with:

game.Update(&fps, &update, &regulate);



I have successfully eliminated half of the errors. I no longer get an error for
"undeclared identifier" for fps, update, and regulate.

It still doesn't like my function calls though. Any ideas?

This post has been edited by BlackPhoenix: 23 August 2009 - 06:42 PM

Was This Post Helpful? 0
  • +
  • -

#3 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: Accessing class object in another .cpp file

Posted 23 August 2009 - 09:00 PM

It would help if you would attach the *.cpp and *.h files to your post so that we can see the entire code instead of some meaningless code snippets, and try to compile them ourselves.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1