• Advertisement
Sign in to follow this  

How do i accomplish this procedure

This topic is 4181 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

Ok first of all I've got to admit that i'm rather bad at c++, so if my question doesn't make sense, shame on me :) Well, i'm coding a simple 2D engine and i'm trying to make an array or vector out of a class in another class but it doesn't seem to work, i'm getting all kinds of errors like "doesn't name a type", "invalid use of undefined struct cMusic". I just want to be able to use the object in an array in another class's function, but it seems like it's bit tricky. The object that i'm trying to include into the GameCodeApp class is the cMusic object. Here's the code (sorry for the mess): GameCodeApp.h
#include <iostream>
#include <vector>

class cGameCodeApp
{
//friend class cMusic;

protected:
    bool m_bFullScreen;
    bool m_bMusic;
    bool m_bIsRunning;
    bool m_bQuitRequested;
    bool m_bQuitting;
    bool m_bMouseCursor;
    bool m_bDontLoad;
    int m_iColorDepth;
    int m_Width;
    int m_Height;
    int m_MusicFiles;
    char *m_GameName;
    char *m_IniFile;
    void ReadSettings();
    void ReadSoundSettings();
    void BuildInfo();
    void InitSDL();
    void ApplySettings();
    void FlashWhileMinimized();
    cMusic *m_Music;

public:
    cGameCodeApp();
    ~cGameCodeApp();
    char* GetTitle();
    char* GetCurrentTime();
    void Init();
    void Shutdown();
    void ToggleFullscreen();
    bool GetScreenState();
    bool IsRunning() { return m_bIsRunning; }
    bool DontLoadSettingsFromIni();
    bool MouseCursorState();
    int GetHeight();
    int GetWidth();
};


GameCodeApp.cpp
#include "windows.h"
#include "GameCodeApp.h"
#include <iostream>
#include "SDL.h"
#include <time.h>

void cGameCodeApp::ApplySettings()
{



}

void cGameCodeApp::ReadSoundSettings()
{
m_IniFile = "./sound.ini";
m_MusicFiles = GetPrivateProfileInt("MUSIC", "Count", 0, m_IniFile);

int i;
if (m_MusicFiles > 0)
    for(i=0;i<m_MusicFiles;i++)
        {
        m_Music = new cMusic;
        m_Music->SetTitle("TEST"); 
        }

}

void cGameCodeApp::ReadSettings()
{
m_IniFile = "./game.ini";
m_Width = GetPrivateProfileInt("OPTIONS", "ResolutionX", 640, m_IniFile);
m_Height = GetPrivateProfileInt("OPTIONS", "ResolutionY", 480, m_IniFile);
m_bFullScreen = GetPrivateProfileInt("OPTIONS", "FullScreen", true, m_IniFile);
m_bMusic = GetPrivateProfileInt("OPTIONS", "Music", true, m_IniFile);
m_bMouseCursor = GetPrivateProfileInt("OPTIONS", "ShowCursor", false, m_IniFile);
m_bDontLoad = GetPrivateProfileInt("OPTIONS", "DontLoad", false, m_IniFile);
std::cout << "Successfully loaded the settings..." << std::endl;
}

void cGameCodeApp::InitSDL()
{
atexit(SDL_Quit);
m_GameName = GAME_NAME;
}

void cGameCodeApp::BuildInfo()
{
std::cout << ENGINE_NAME << " " << ENGINE_VERSION << std::endl
     << "Built on " << __DATE__ << std::endl
     << __TIME__ << std::endl
     << "------------------------" << std::endl
     << "Initializing " << GAME_NAME << " " << GAME_VERSION << std::endl;

}

char* cGameCodeApp::GetTitle()
{
return m_GameName;
}

char* cGameCodeApp::GetCurrentTime()
{
char* systime;
SYSTEMTIME time;
GetLocalTime(& time);
//sprintf(systime, "%i:%i", time.wHour, time.wMinute);
return systime;
}

bool cGameCodeApp::GetScreenState()
{
return m_bFullScreen;
}

int cGameCodeApp::GetHeight()
{
return m_Height;
}

int cGameCodeApp::GetWidth()
{
return m_Width;
}

bool cGameCodeApp::MouseCursorState()
{
return m_bMouseCursor;
}

bool cGameCodeApp::DontLoadSettingsFromIni()
{
return m_bDontLoad;
}

cGameCodeApp::cGameCodeApp()
{
BuildInfo();
ReadSettings();
InitSDL();
std::cout << "GameCodeApp initialized successfully" << std::endl;
}

cGameCodeApp::~cGameCodeApp()
{
std::cout << "GameCodeApp destroyed successfully" << std::endl;
};




Music.h
class cMusic
{
public:
friend class SoundSystem;
friend class GameCodeApp;
friend class MusicPlayer;
  //  cMusic();

    void PlayMusic();
    void LoadMusic(char *MFile);
    void SetRepeat(int Count);

    void SetTitle(std::string Title);
    std::string GetTitle();
    char* GetPath();

    void Pause();
    void Stop();


private:
    Mix_Music *m_Stream;
    std::string m_SongName;
    char* m_Path;
};

void cMusic::PlayMusic()
{
Mix_PlayMusic(m_Stream, 0);
}

char* cMusic::GetPath()
{
return m_Path;
}

void cMusic::SetTitle(std::string Title)
{
m_SongName = Title;
std::cout << "Song title of " << m_Path << " is now " << Title << std::endl;
}

std::string cMusic::GetTitle()
{
return m_SongName;
}

void cMusic::LoadMusic(char *MFile)
{
m_Stream = Mix_LoadMUS(MFile);
m_Path = MFile;
std::cout << "Loaded music file: " << MFile << std::endl;
}


Thanks. [Edited by - Ghosthack on August 12, 2006 7:12:47 PM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Ghosthack
Well, i'm coding a simple 2D engine and i'm trying to make an array or vector out of a class in another class but it doesn't seem to work, i'm getting all kinds of errors like "doesn't name a type", "invalid use of undefined struct cMusic".


Add

#include "Music.h"

to the list of includes of GameCodeApp.h

i.e.

#include <iostream>
#include <vector>
#include "Music.h"

Share this post


Link to post
Share on other sites
I've tried that and the only thing i'm getting is errors...

.objs\GameCodeApp.o:GameCodeApp.cpp:(.text+0x100): multiple definition of `cMusic::PlayMusic()'
.objs\Engine.o:Engine.cpp:(.text+0x100): first defined here
.objs\GameCodeApp.o:GameCodeApp.cpp:(.text+0x11e): multiple definition of `cMusic::GetPath()'
.objs\Engine.o:Engine.cpp:(.text+0x11e): first defined here
.objs\GameCodeApp.o:GameCodeApp.cpp:(.text+0x12a): multiple definition of `cMusic::SetTitle(std::string)'
.objs\Engine.o:Engine.cpp:(.text+0x12a): first defined here
.objs\GameCodeApp.o:GameCodeApp.cpp:(.text+0x1a0): multiple definition of `cMusic::GetTitle()'
.objs\Engine.o:Engine.cpp:(.text+0x1a0): first defined here
.objs\GameCodeApp.o:GameCodeApp.cpp:(.text+0x1c6): multiple definition of `cMusic::LoadMusic(char*)'
.objs\Engine.o:Engine.cpp:(.text+0x1c6): first defined here
.objs\MusicPlayer.o:MusicPlayer.cpp:(.text+0x100): multiple definition of `cMusic::PlayMusic()'
.objs\Engine.o:Engine.cpp:(.text+0x100): first defined here
.objs\MusicPlayer.o:MusicPlayer.cpp:(.text+0x11e): multiple definition of `cMusic::GetPath()'
.objs\Engine.o:Engine.cpp:(.text+0x11e): first defined here
.objs\MusicPlayer.o:MusicPlayer.cpp:(.text+0x12a): multiple definition of `cMusic::SetTitle(std::string)'
.objs\Engine.o:Engine.cpp:(.text+0x12a): first defined here
.objs\MusicPlayer.o:MusicPlayer.cpp:(.text+0x1a0): multiple definition of `cMusic::GetTitle()'
.objs\Engine.o:Engine.cpp:(.text+0x1a0): first defined here
.objs\MusicPlayer.o:MusicPlayer.cpp:(.text+0x1c6): multiple definition of `cMusic::LoadMusic(char*)'
.objs\Engine.o:Engine.cpp:(.text+0x1c6): first defined here
.objs\VideoSystem.o:VideoSystem.cpp:(.text+0x100): multiple definition of `cMusic::PlayMusic()'
.objs\Engine.o:Engine.cpp:(.text+0x100): first defined here
.objs\VideoSystem.o:VideoSystem.cpp:(.text+0x11e): multiple definition of `cMusic::GetPath()'
.objs\Engine.o:Engine.cpp:(.text+0x11e): first defined here
.objs\VideoSystem.o:VideoSystem.cpp:(.text+0x12a): multiple definition of `cMusic::SetTitle(std::string)'
.objs\Engine.o:Engine.cpp:(.text+0x12a): first defined here
.objs\VideoSystem.o:VideoSystem.cpp:(.text+0x1a0): multiple definition of `cMusic::GetTitle()'
.objs\Engine.o:Engine.cpp:(.text+0x1a0): first defined here
.objs\VideoSystem.o:VideoSystem.cpp:(.text+0x1c6): multiple definition of `cMusic::LoadMusic(char*)'
.objs\Engine.o:Engine.cpp:(.text+0x1c6): first defined here
.objs\main.o:main.cpp:(.text+0x100): multiple definition of `cMusic::PlayMusic()'
.objs\Engine.o:Engine.cpp:(.text+0x100): first defined here
.objs\main.o:main.cpp:(.text+0x11e): multiple definition of `cMusic::GetPath()'
.objs\Engine.o:Engine.cpp:(.text+0x11e): first defined here
.objs\main.o:main.cpp:(.text+0x12a): multiple definition of `cMusic::SetTitle(std::string)'
.objs\Engine.o:Engine.cpp:(.text+0x12a): first defined here
.objs\main.o:main.cpp:(.text+0x1a0): multiple definition of `cMusic::GetTitle()'
.objs\Engine.o:Engine.cpp:(.text+0x1a0): first defined here
.objs\main.o:main.cpp:(.text+0x1c6): multiple definition of `cMusic::LoadMusic(char*)'
.objs\Engine.o:Engine.cpp:(.text+0x1c6): first defined here
collect2: ld returned 1 exit status

Share this post


Link to post
Share on other sites
There are cases where you can include member function definitions for a class in the header file (class or function templates and inline functions, for example), but this is not the case in your example.

Try moving the function definitions for cMusic to a separate .cpp file; that should fix the problem.

[Edited by - jyk on August 12, 2006 6:19:22 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Did you read this yet?


Nope, i'm going to do it right away ^^

Quote:
Original post by jyk
There are cases where you can include member function definitions for a class in the header file (class or function templates and inline functions, for example), but this is not the case in your example.

Try moving the function definitions for cMusic to a separate .cpp file; that should fix the problem.


Thanks it seems to work, but yet not, when I try to use it in this function it doesn't work

void cGameCodeApp::ReadSoundSettings()
{
m_IniFile = "./sound.ini";
m_MusicFiles = GetPrivateProfileInt("MUSIC", "Count", 0, m_IniFile);

int i;
if (m_MusicFiles > 0)
for(i=0;i<m_MusicFiles;i++)
{
// THESE ARE THE LINES THAT I ADDED JUST NOW
m_Music = new cMusic;
m_Music->SetTitle("TEST");
/////////////////////////////////////////////
}

}



I get the following:

.objs\GameCodeApp.o:GameCodeApp.cpp:(.text+0x26e): undefined reference to `cMusic::SetTitle(std::string)'
collect2: ld returned 1 exit status

Share this post


Link to post
Share on other sites
You might update your original post to reflect the current state of the code; that will make it easier to spot the problem.

Also, it doesn't look like your header files have include guards, which may cause you problems at some point.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement