Click here to Skip to main content
15,896,730 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I trying to self-learn C++ and don't have much idea about header files. When I tried to create one as follows, I got a strange error that I do not understand. I have tried googling but my lack of understanding about C++ compiling/linking process is an obstacle.


Anyway, here is the header file (unnecessary code removed)

GraphicsLib.h

C++
#ifndef GRAPHICS_LIB_INCLUDE
#define GRAPHICS_LIB_INCLUDE

enum BorderTypes
{
SINGLE_ALL,
DOUBLE_ALL,
SINGLE_SIDE_DOUBLE_TOP,
DOUBLE_SIDE_SINGLE_TOP
};

static void DrawMessageWithBox(std::string message, BorderTypes borderType, int length);
#endif // GRAPHICS_LIB_INCLUDE


The GraphicsLib.cpp file

C++
include "GraphicsLib.h"

static void DrawMessageWithBox(string message, BorderTypes borderType, int length)
{
    //Draw the box and the message

}


The main program

C++
#include<iostream>
#include "GraphicsLib.h"
using namespace std;

int main()
{
   DrawMessageWithBox("HELLO WORLD", BorderTypes.DOUBLE_ALL, 77);

   return 0;
}



Whenever I try to compile the program, I get this error -

"[ERROR] Expected primary-expression before '.' token"

If I replace the function call with this one,
C++
DrawMessageWithBox("HELLO WORLD", DOUBLE_ALL, 77);


I get the following error -
"[Warning] 'void DrawCenteredMessageWithBox(std::string, BorderTypes, int)' used but never defined [enabled by default]"
In function 'main':
"undefined reference to `DrawCenteredMessageWithBox(std::string, BorderTypes, int)' "


Can anyone please tell me what am I doing wrong?
Thanks.
Posted

In the first case you do not need the BorderTypes. prefix on your enum values. In the second case you have a call to DrawCenteredMessageWithBox somewhere (not shown), but that function has never been defined.

[edit]
See also Jochen's Solution entry.
[/edit]
 
Share this answer
 
v2
With enums you must use two colons between the name and the enumerator:
DrawMessageWithBox("HELLO WORLD", BorderTypes::DOUBLE_ALL, 77);
 
Share this answer
 
Quote:
"[Warning] 'void DrawCenteredMessageWithBox(std::string, BorderTypes, int)' used but never defined [enabled by default]"
In function 'main':
"undefined reference to `DrawCenteredMessageWithBox(std::string, BorderTypes, int)' "

You cannot access static functions outside their source file. Remove the static qualifier in order to gain access to it.
 
Share this answer
 
Thanks for your replies.

Nothing seems to work till now.

C++
DrawCenteredMessageWithBox
is a well defined function and is indeed called from
C++
DrawMessageWithBox

[I omitted it in the code provided for the sake of simplification.]


Still working on it. I will let you know if I can find the problem. I might have made a mistake somewhere else.
Thanks again.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900