Namespace HELP

Hello CPP Community,

It's the first time I'm using C++ namespaces and I am having trouble making my main.cpp find the functions... Is there something I'm missing? (All files are in the project)

Thank you in advance!

Error:

"main.cpp|16|undefined reference to `fileManage::open(fileManage::ArchiveInfo*, char const*)"


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//main.cpp
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "fileManage.h"
#define FILENAME01 "hola.txt"
using namespace fileManage;

int main()
{
    fileManage::ArchiveInfo firstArch;

    firstArch.fileName = FILENAME01;

    if(fileManage::open(&firstArch,"rw"))
    {
        printf("The file is open\n");
    }

  //fclose();
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//  fileManage.h
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define FILEMANAGE_TRUE 1
#define FILEMANAGE_FALSE 0

namespace fileManage
{
    typedef struct
    {
        unsigned int fileSize;
        const char * fileName;
        FILE * pFile;
    }ArchiveInfo;

    bool open(ArchiveInfo * fileData,const char * mode);
};

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//  fileManage.cpp
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

#include "fileManage.h"
using namespace fileManage;
bool open(ArchiveInfo * fileData,const char * mode)
{
    if(fileData->fileName != NULL)
    {
        fileData->pFile = fopen(fileData->fileName,mode);
        return FILEMANAGE_TRUE;
    }
    return FILEMANAGE_FALSE;
}   
i haven't worked with namespaces much, but my thought is you should do the same think as with classes and declaring functions outside the class
In your case
1
2
3
4
5
6
7
8
9
bool fileManage::open(ArchiveInfo* fileData, const char * mode)
{
    if(fileData->fileName != NULL)
    {
        fileData->pFile = fopen(fileData->fileName,mode);
        return FILEMANAGE_TRUE;
    }
    return FILEMANAGE_FALSE;
}
As nedo has already pointed out, you need to define the function fileManage::open() in fileManager.cpp, not ::open() as you have done so.

I usually use the same approach as nedo, explictly prefixing the name space. But some people use this approach (i.e. put the function definition(s) in the namespace.) I do use this second approach for small, usually helper, functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
namespace fileManage {

bool open(ArchiveInfo * fileData,const char * mode)
{
    if(fileData->fileName != NULL)
    {
        fileData->pFile = fopen(fileData->fileName,mode);
        return FILEMANAGE_TRUE;
    }
    return FILEMANAGE_FALSE;
}

} // end namespace fileManage 


Andy
Last edited on
Yeah, separating the functions definitions from the namespace is better since the code is easier to read when projects become bigger as you pointed out.

Thank you very much for the help! It now works smoothly!
Topic archived. No new replies allowed.