LNK2005和LNK1169错误 - C ++

时间:2018-04-08 21:43:26

标签: c++ linker

代码是自我解释的,它涉及我测试命名空间,它涉及用户输入三个数字,保存到x y和z,然后数字全部相乘,然后全部相互划分

我收到这三个错误

reg.students[0].Idea = reg.idea;

stdafx.h

LNK2005 "int __cdecl Div::doTheMath(int,int,int)" (?doTheMath@Div@@YAHHHH@Z) already defined in Namespacedemo.obj   
LNK2005 "int __cdecl Mul::doTheMath(int,int,int)" (?doTheMath@Mul@@YAHHHH@Z) already defined in Namespacedemo.obj
LNK1169 one or more multiply defined symbols found

Divide.h

#pragma once
#include "Divide.h"
#include "Multiply.h"
#include <iostream>    
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>

Multiply.h

#pragma once
namespace Div
{
    int doTheMath(int x, int y, int z)
    {
        int w = x / y / z;
        return w;
    }
}

Namespacedemo.cpp

#pragma once
#pragma once
namespace Mul
{
    int doTheMath(int x, int y, int z)
    {
        int w = x * y * z;
        return w;
    }
}

1 个答案:

答案 0 :(得分:0)

这就是你的文件的样子:

Divide.h

#pragma once

namespace Div
{
    int doTheMath(int x, int y, int z);
}

Multiply.h

#pragma once

namespace Mul
{
    int doTheMath(int x, int y, int z);
}

Divide.cpp

#include "Divide.h"

namespace Div
{
    int doTheMath(int x, int y, int z)
    {
        int w = x / y / z;
        return w;
    }
}

Multiply.cpp

#include "Multiply.h"

namespace Mul
{
    int doTheMath(int x, int y, int z)
    {
        int w = x * y * z;
        return w;
    }
}

稍后详细说明......