2016-04-12 7 views
0

以下のコードを参照してください。リンカがMemoryの機能を見つけることができないと不平を言っているので、何か問題がありますが、私は理由を理解できません。リンカーは名前空間の関数を見つけることができません

memory.h

#pragma once 
#include "includes.h" //it just includes other strandard headers. 

class MemoryUnit 
{ 
public: 
    MemoryUnit() {} 
    virtual int getValue() = 0; 
    virtual int getSize() = 0; 
    virtual void setValue(int) = 0; 
    virtual ~MemoryUnit() {}; 
}; 
class Byte : public MemoryUnit 
{ 
    int value; 
public: 
    static int size; 
    Byte(int byte) :value(byte) {}; 
    int getSize() { return size; } 
    int getValue() { return value; }; 
    void setValue(int byte) { value = byte; } 
    ~Byte() {}; 
}; 
namespace Memory 
{ 
    extern int size; 
    extern MemoryUnit** map; 
    void checkAddress(int address); 
    int read(int adress); 
    MemoryUnit* getOperation(int address); 
    void write(int adress, MemoryUnit* data); 
    void writeByte(int adress, int data); 
} 

memory.cpp

#include "includes.h" 
#include "memory.h" 
#include "simulator.h" // it contains only externed constants. 

namespace Memory 
{ 
    int size = 0; 
    MemoryUnit** map = NULL; 
    inline MemoryUnit* getOperation(int address) 
    { 
     return map[address]; 
    } 
    inline void checkAddress(int address) 
    { 
     if (address < 0 || address >= MAX_MEMORY_SIZE) 
      throw std::out_of_range("Invalid memory address."); 
    } 
    inline int read(int address) 
    { 
     checkAddress(address); 
     return map[address]->getValue(); 
    } 
    inline void write(int address, MemoryUnit* data) 
    { 
     checkAddress(address); 
     delete map[address]; 
     map[address] = data; 
    } 
    inline void writeByte(int address, int data) 
    { 
     checkAddress(address); 
     map[address]->setValue(data); 
    } 
} 

クラス/名前空間memory.hは宣言するどこにでもあるmemory.hが含まれています。下のコードで何か間違っていますか?

編集:最初のエラーのため

LNK1120 5 unresolved externals simulator.exe 
LNK2019 unresolved external symbol "void __cdecl Memory::writeByte(int,int)" referenced in function "void __cdecl ALU::setFlags(int)" alu.obj 
LNK2001 unresolved external symbol "void __cdecl Memory::writeByte(int,int)" cu.obj 
LNK2019 unresolved external symbol "class MemoryUnit * __cdecl Memory::getOperation(int)" referenced in function "void __cdecl CU::run(void)" cu.obj 
LNK2001 unresolved external symbol "void __cdecl Memory::writeByte(int,int)" helpers.obj 
LNK2019 unresolved external symbol "void __cdecl Memory::write(int,class MemoryUnit *)" referenced in function "void __cdecl readProgramCommands(void)" helpers.obj 
LNK2001 unresolved external symbol "public: virtual int __thiscall MemoryPointer::getValue(void)" helpers.obj 
LNK2001 unresolved external symbol "public: virtual int __thiscall IndirectMemoryPointer::getAddress(void)" helpers.obj 
LNK2001 unresolved external symbol "void __cdecl Memory::writeByte(int,int)" main.obj 

alu.halu.cpp

//alu.h 
#pragma once 
#include "includes.h" 
#include "operation.h" 
namespace ALU 
{ 
    int operation(Operation* op); 
    void setFlags(int result); 
} 
//alu.cpp 
#include "includes.h" 
#include "simulator.h" 
#include "alu.h" 
#include "memory.h" 
#include "operation.h" 
namespace ALU 
{ 
    int operation(Operation* operation) 
    { 
     // ... 
     setFlags(result); 
     return result; 
    } 
    inline void setFlags(int result) 
    { 
     Memory::writeByte(FLAG_Z, result == 0); 
     // ... 
    } 
} 
+0

正確なエラーメッセージとエラーを出力する行を入力できますか?また、あなたもmemory.cppをコンパイルし、それに対してリンクしていますか? – aybassiouny

+0

@aybassiouny質問を編集しました。すべてのファイルがコンパイルされ、リンクはVSのジョブです。 – klenium

答えて

1

あなたはインライン関数やメソッドを使用している場合は、それらの定義は、それらを使用するすべてのソースユニットのために表示されるはずです。あなたは、インライン関数をMemory.cppに定義しました。そのため、リンカーエラーが「未解決」になってしまいます。することができますあなたの問題を解決するために

  1. はインライン修飾子を削除し、Memory.cppの関数の定義を保持します。

  2. インライン修飾子を保持しますが、関数定義をMemory.hに移動します。

+0

'inline'は、できるだけ速くなければならないので、私は' inline'をそのまま使用します。しかし、ヘッダーにgetter/setter以外の記述はしていませんが、ヘッダー内のより複雑な関数を定義することをお勧めしますか? – klenium

+0

もし複雑すぎると、コンパイラはあなたの 'inline'指定子を無視します。パフォーマンスがプログラムにとって重要な要素であれば、インラインにしておくと害はありません。 – CodeFuller

2

あなたが配置する必要が 私は、Visual Studioにプロジェクトをビルドするときに私が得た2015年 エラーを使用していますヘッダーファイル内のインライン関数定義(両方とも使用されているすべての翻訳単位に表示されている必要があります)では、宣言と定義を分けることができますが、両方ともヘッダーファイルになければなりません。また、インラインで宣言する必要があります。 7.1.2.4

dcl.fct.spec

N4140

An inline function shall be defined in every translation unit in which it is odr-used and shall have exactly the same definition in every case (3.2). [ Note: A call to the inline function may be encountered before its definition appears in the translation unit. —end note ] If the definition of a function appears in a translation unit before its first declaration as inline, the program is ill-formed.

関連する問題