Connecting Tech Pros Worldwide Help | Site Map

Trouble with namespaces and unresolved externals

  #1  
Old July 22nd, 2005, 06:58 PM
Ney André de Mello Zunino
Guest
 
Posts: n/a
Hello.

I am having trouble with namespaces and unresolved externals. The
following three files (memoria.h, memoria.cpp and teste.cpp) illustrate
the problem:

// *************** memoria.h ***************

#include <vector>

namespace mips
{
using std::vector;
using std::size_t;
const unsigned int c_tam_memoria = 65536;

class memoria
{
public:
memoria(const size_t tamanho = c_tam_memoria);
ubyte& operator[](const size_t posicao);
private:
vector<ubyte> m_memoria;
};
}

// ***************end of memoria.h ***************


// *************** memoria.cpp ***************

#include "memoria.h"

namespace mips
{

memoria::memoria(const size_t tamanho)
: m_memoria(tamanho)
{
}

ubyte& memoria::operator[](const size_t posicao)
{
return m_memoria.at(posicao);
}

}

// *************** end of memoria.cpp ***************


// *************** teste.cpp ***************

#include "memoria.h"

int main()
{
using namespace mips;
memoria mem;
mem[100] = 33;
}

// *************** end of teste.cpp ***************

Could anybody tell me what I am missing? I thought I had grasped the
concept of namespaces, but it seems I have not. I would appreciate if
someone could show me where I am wrong.

Thank you very much,

--
Ney André de Mello Zunino
  #2  
Old July 22nd, 2005, 06:58 PM
Victor Bazarov
Guest
 
Posts: n/a

re: Trouble with namespaces and unresolved externals


"Ney André de Mello Zunino" <zunino@inf.ufsc.br> wrote...[color=blue]
> I am having trouble with namespaces and unresolved externals. The
> following three files (memoria.h, memoria.cpp and teste.cpp) illustrate
> the problem:
>
> // *************** memoria.h ***************
>
> #include <vector>
>
> namespace mips
> {
> using std::vector;
> using std::size_t;
> const unsigned int c_tam_memoria = 65536;
>
> class memoria
> {
> public:
> memoria(const size_t tamanho = c_tam_memoria);
> ubyte& operator[](const size_t posicao);
> private:
> vector<ubyte> m_memoria;
> };
> }
>
> // ***************end of memoria.h ***************
>
>
> // *************** memoria.cpp ***************
>
> #include "memoria.h"
>
> namespace mips
> {
>
> memoria::memoria(const size_t tamanho)
> : m_memoria(tamanho)
> {
> }
>
> ubyte& memoria::operator[](const size_t posicao)
> {
> return m_memoria.at(posicao);
> }
>
> }
>
> // *************** end of memoria.cpp ***************
>
>
> // *************** teste.cpp ***************
>
> #include "memoria.h"
>
> int main()
> {
> using namespace mips;
> memoria mem;
> mem[100] = 33;
> }
>
> // *************** end of teste.cpp ***************
>
> Could anybody tell me what I am missing? I thought I had grasped the
> concept of namespaces, but it seems I have not. I would appreciate if
> someone could show me where I am wrong.[/color]

If the code doesn't compile, you have to post the compiler diagnostics.
I tried compiling it, and it passed with flying colours. If the program
doesn't link, did you remember to add both files to the link command line?

Anyway, post the error messages you get. The code seems fine, but there
is always a possibility of a compiler malfunction...

Victor


  #3  
Old July 22nd, 2005, 06:58 PM
Ney André de Mello Zunino
Guest
 
Posts: n/a

re: Trouble with namespaces and unresolved externals


Victor Bazarov wrote:
[color=blue]
> If the code doesn't compile, you have to post the compiler diagnostics.
> I tried compiling it, and it passed with flying colours. If the program
> doesn't link, did you remember to add both files to the link command line?[/color]

Victor, I thank you for asking that question about adding the object
files to the link command line because, believe it, that was precisely
what I missed. I feel quite ashamed...

Best regards,

--
Ney André de Mello Zunino
  #4  
Old July 22nd, 2005, 06:58 PM
Julián Albo
Guest
 
Posts: n/a

re: Trouble with namespaces and unresolved externals


Ney André de Mello Zunino wrote:
[color=blue]
> I am having trouble with namespaces and unresolved externals. The
> following three files (memoria.h, memoria.cpp and teste.cpp) illustrate
> the problem:[/color]

You dont't define what a ubyte is. Defining it, your code compiles.

--
Salu2
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ coding standarts and how to structure "bigger" projects Mikkel christensen answers 4 July 22nd, 2005 10:33 PM