Jump to content
  • Advertisement
Sign in to follow this  

Multiple definition problems

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

First off, here is my code: main.cpp
/************************************************************
Project:      HoB: Human or Beast
File:         main.cpp
Function:     Main
Description:  The core of HoB's functionality
Author:       Paul Adamski (PA)
Environment:  Dev C++ 4.9.9.2
              Integrated Graphics Card, 256mb RAM, Windows 2000
TODO List:    Text color function
              Header/Library file to hold all the functions and includes
Revisions:    0.01           06/18/2006 (PA) First development main.cpp
                             Created an extremely basic file, prints out "HoB: Human or Beast" 
                             at the center of the screen, added a few comments and a TODO list
              0.01.1         06/18/2006 (PA) Minor addition main.cpp
                             Added the include string to make use of string instead of char[0] 
              0.05           06/19/2006 (PA) Addition main.cpp, library.h
                             Added library.h to act as the library, to hold the globals, functions, 
                             constants, etc., added the txtcolor() function, added the clrscreen()
                             function, and gave the console a title. 
              0.06           07/03/2006 (PA) Addition main.cpp, library.h, cutscene.cpp
                             Added a few minor lines, reorganized a few code, and got the cutscene 
                             part started.      
              0.07           07/07/2006 (PA) Addition/Revamp main.cpp, library.h, cutscene.cpp, txtcolor.cpp, clrscreen.cpp
                             Revamped the organization better. Moved the txtcolor and clrscreen functions 
                             from library to their own sections allowing for easy future code reuse.
************************************************************/
#include "library.h"

char* consoletitle = "HoB: Human or Beast";

void intro();

int main()
{
    SetConsoleTitle(consoletitle);
    //I used to have consoletitle.c_str() here and std::string consoletitle above
    //.c_str() is a function that makes the string variable consoletitle a char* variable
    
    txtcolor("lred");
    std::cout << "                              HoB: Human or Beast" << std::endl;
    txtcolor("red");
    std::cout << "                   Programmed and Designed by: Paul Adamski" << std::endl;
    std::cin.ignore();
    
    intro();
}

library.h
/************************************************************
Project:      HoB: Human or Beast
File:         main.cpp
Function:     Main
Description:  The core of HoB's functionality
Author:       Paul Adamski (PA)
Environment:  Dev C++ 4.9.9.2
              Integrated Graphics Card, 256mb RAM, Windows 2000
TODO List:    Text color function
              Header/Library file to hold all the functions and includes
Revisions:    0.01           06/18/2006 (PA) First development main.cpp
                             Created an extremely basic file, prints out "HoB: Human or Beast" 
                             at the center of the screen, added a few comments and a TODO list
              0.01.1         06/18/2006 (PA) Minor addition main.cpp
                             Added the include string to make use of string instead of char[0] 
              0.05           06/19/2006 (PA) Addition main.cpp, library.h
                             Added library.h to act as the library, to hold the globals, functions, 
                             constants, etc., added the txtcolor() function, added the clrscreen()
                             function, and gave the console a title. 
              0.06           07/03/2006 (PA) Addition main.cpp, library.h, cutscene.cpp
                             Added a few minor lines, reorganized a few code, and got the cutscene 
                             part started.      
              0.07           07/07/2006 (PA) Addition/Revamp main.cpp, library.h, cutscene.cpp, txtcolor.cpp, clrscreen.cpp
                             Revamped the organization better. Moved the txtcolor and clrscreen functions 
                             from library to their own sections allowing for easy future code reuse.
************************************************************/
#include <iostream>
#include <windows.h>
#include <string>
#include "txtcolor.cpp"
#include "clrscreen.cpp"

cutscene.cpp
/************************************************************
Project:      HoB: Human or Beast
File:         main.cpp
Function:     Main
Description:  The core of HoB's functionality
Author:       Paul Adamski (PA)
Environment:  Dev C++ 4.9.9.2
              Integrated Graphics Card, 256mb RAM, Windows 2000
TODO List:    Text color function
              Header/Library file to hold all the functions and includes
Revisions:    0.01           06/18/2006 (PA) First development main.cpp
                             Created an extremely basic file, prints out "HoB: Human or Beast" 
                             at the center of the screen, added a few comments and a TODO list
              0.01.1         06/18/2006 (PA) Minor addition main.cpp
                             Added the include string to make use of string instead of char[0] 
              0.05           06/19/2006 (PA) Addition main.cpp, library.h
                             Added library.h to act as the library, to hold the globals, functions, 
                             constants, etc., added the txtcolor() function, added the clrscreen()
                             function, and gave the console a title. 
              0.06           07/03/2006 (PA) Addition main.cpp, library.h, cutscene.cpp
                             Added a few minor lines, reorganized a few code, and got the cutscene 
                             part started.      
              0.07           07/07/2006 (PA) Addition/Revamp main.cpp, library.h, cutscene.cpp, txtcolor.cpp, clrscreen.cpp
                             Revamped the organization better. Moved the txtcolor and clrscreen functions 
                             from library to their own sections allowing for easy future code reuse.
************************************************************/
#include "library.h"

void clrscreen();

void intro()
{
     clrscreen();
     std::cout << "This is an introduction test.";
     std::cin.ignore();
}

txtcolor.cpp
#ifndef TXTCOLOR_CPP
#define TXTCOLOR_CPP
#include <windows.h>
#include <string>
#endif

void txtcolor(std::string color)
{
    if (color == "red")
    {
              SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 
              FOREGROUND_RED);              
    }

    if (color == "green")
    {
              SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 
              FOREGROUND_GREEN);              
    }

    if (color == "blue")
    {
              SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 
              FOREGROUND_BLUE);              
    }
    
    if (color == "cyan")
    {
              SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 
              FOREGROUND_GREEN | FOREGROUND_BLUE);              
    }

    if (color == "purple")
    {
              SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 
              FOREGROUND_RED | FOREGROUND_BLUE);              
    }

    if (color == "yellow")
    {
              SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 
              FOREGROUND_RED | FOREGROUND_GREEN);              
    }

    if (color == "dgrey")
    {
              SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 
              FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);              
    }

    if (color == "lgrey")
    {
              SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 
              FOREGROUND_INTENSITY);              
    }

    if (color == "lred")
    {
              SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 
              FOREGROUND_INTENSITY | FOREGROUND_RED);              
    }

    if (color == "lgreen")
    {
              SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 
              FOREGROUND_INTENSITY | FOREGROUND_GREEN);              
    }

    if (color == "lblue")
    {
              SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 
              FOREGROUND_INTENSITY | FOREGROUND_BLUE);              
    }

    if (color == "lcyan")
    {
              SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 
              FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);              
    }

    if (color == "lpurple")
    {
              SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 
              FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);              
    }

    if (color == "lyellow")
    {
              SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 
              FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);              
    }

    if (color == "white")
    {
              SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 
              FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);              
    }
}

clrscreen.cpp
#ifndef CLRSCREEN_CPP
#define CLRSCREEN_CPP
#include <windows.h>
#endif

void clrscreen() 
{ 
    COORD coordScreen = { 0, 0 }; 
    DWORD cCharsWritten; 
    CONSOLE_SCREEN_BUFFER_INFO csbi; 
    DWORD dwConSize; 
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); 
    
    GetConsoleScreenBufferInfo(hConsole, &csbi); 
    dwConSize = csbi.dwSize.X * csbi.dwSize.Y; 
    FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, 
        coordScreen, &cCharsWritten); 
    GetConsoleScreenBufferInfo(hConsole, &csbi); 
    FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, 
        coordScreen, &cCharsWritten); 
    SetConsoleCursorPosition(hConsole, coordScreen); 
}

Now, the problem is, when I compile it, I get "multiple definition of 'txtcolor(std::string)' and "multiple definition of 'clrscreen()'".

Share this post


Link to post
Share on other sites
Advertisement
This is probably due to your include files being included several times over. Your compiler is trying to overload those functions, but fails because the arguments are exactly the same.
Try putting the code in the header files in #defines, like

#ifndef HEADER_H //Name dosen't matter here; just my coding practice
#define HEADER_H

//code in the header file

#endif

Share this post


Link to post
Share on other sites
You appear to use include guards (#ifndef ... #define ... #endif), so I'll give you a pat on the back. However, the structure of your project is not what most of us are used to, and I suspect it may be the source of your problems.

Use header files to declare your functions. Usually, there are several function declarations per header file, and the functions are closely related (the name of the header file should reflect what kind of functions are in the file). If you want to use a particular function in a source file, you #include the header file that contains its declaration.

Then, you put the actual function definition in a source file (this source file should probably have the same name as the header file).

Use include guards in the header file, but not the source file.

Finally, never #include a .cpp file. No one will shoot you for it, but it defeats the purpose of having the header/source file distinction.

Share this post


Link to post
Share on other sites
Awesome, thanks for the help Vodka! Here is my current code just incase someone has the same problem:

main.cpp:
/************************************************************
Project: HoB: Human or Beast
File: main.cpp
Function: Main
Description: The core of HoB's functionality
Author: Paul Adamski (PA)
Environment: Dev C++ 4.9.9.2
Integrated Graphics Card, 256mb RAM, Windows 2000
TODO List: Finish the game, minus the combat
Revisions: 0.01 06/18/2006 (PA) First development main.cpp
Created an extremely basic file, prints out "HoB: Human or Beast"
at the center of the screen, added a few comments and a TODO list
0.01.1 06/18/2006 (PA) Minor addition main.cpp
Added the include string to make use of string instead of char[0]
0.05 06/19/2006 (PA) Addition main.cpp, library.h
Added library.h to act as the library, to hold the globals, functions,
constants, etc., added the txtcolor() function, added the clrscreen()
function, and gave the console a title.
0.06 07/03/2006 (PA) Addition main.cpp, library.h, cutscene.cpp
Added a few minor lines, reorganized a few code, and got the cutscene
part started.
0.07 07/07/2006 (PA) Addition/Revamp main.cpp, cutscene.cpp, consoleset.h, consoleset.cpp
Revamped the organization better. Moved the txtcolor and clrscreen functions
from library to their own sections allowing for easy future code reuse.
************************************************************/

#include "consoleset.h"

char* consoletitle = "HoB: Human or Beast";

int main()
{
SetConsoleTitle(consoletitle);
//I used to have consoletitle.c_str() here and std::string consoletitle above
//.c_str() is a function that makes the string variable consoletitle a char* variable

txtcolor("lred");
std::cout << " HoB: Human or Beast" << std::endl;
txtcolor("red");
std::cout << " Programmed and Designed by: Paul Adamski" << std::endl;
std::cin.ignore();

cutscene();
}






cutscene.cpp:
/************************************************************
Project: HoB: Human or Beast
File: cutscene.cpp
Function: Cutscene
Description: The first cutscene of the game with no interactivity avaiable
Author: Paul Adamski (PA)
Environment: Dev C++ 4.9.9.2
Integrated Graphics Card, 256mb RAM, Windows 2000
TODO List: Finish the game, minus the combat
Revisions: 0.01 06/18/2006 (PA) First development main.cpp
Created an extremely basic file, prints out "HoB: Human or Beast"
at the center of the screen, added a few comments and a TODO list
0.01.1 06/18/2006 (PA) Minor addition main.cpp
Added the include string to make use of string instead of char[0]
0.05 06/19/2006 (PA) Addition main.cpp, library.h
Added library.h to act as the library, to hold the globals, functions,
constants, etc., added the txtcolor() function, added the clrscreen()
function, and gave the console a title.
0.06 07/03/2006 (PA) Addition main.cpp, library.h, cutscene.cpp
Added a few minor lines, reorganized a few code, and got the cutscene
part started.
0.07 07/07/2006 (PA) Addition/Revamp main.cpp, cutscene.cpp, consoleset.h, consoleset.cpp
Revamped the organization better. Moved the txtcolor and clrscreen functions
from library to their own sections allowing for easy future code reuse.
************************************************************/

#include "consoleset.h"

void cutscene()
{
clrscreen();
std::cout << "This is an introduction test.";
std::cin.ignore();
}






consoleset.h:
#include <iostream>
#include <windows.h>
#include <string>

void cutscene();
void clrscreen();
void txtcolor(std::string color);






consoleset.cpp:
#ifndef CONSOLE_CPP
#define CONSOLE_CPP
#include <windows.h>
#include <string>
#endif

void txtcolor(std::string color)
{
if (color == "red")
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_RED);
}

if (color == "green")
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_GREEN);
}

if (color == "blue")
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_BLUE);
}

if (color == "cyan")
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_GREEN | FOREGROUND_BLUE);
}

if (color == "purple")
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_RED | FOREGROUND_BLUE);
}

if (color == "yellow")
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_RED | FOREGROUND_GREEN);
}

if (color == "dgrey")
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}

if (color == "lgrey")
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY);
}

if (color == "lred")
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_RED);
}

if (color == "lgreen")
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_GREEN);
}

if (color == "lblue")
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_BLUE);
}

if (color == "lcyan")
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);
}

if (color == "lpurple")
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
}

if (color == "lyellow")
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
}

if (color == "white")
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}
}

void clrscreen()
{
COORD coordScreen = { 0, 0 };
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize,
coordScreen, &cCharsWritten);
GetConsoleScreenBufferInfo(hConsole, &csbi);
FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize,
coordScreen, &cCharsWritten);
SetConsoleCursorPosition(hConsole, coordScreen);
}




Share this post


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

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!