代码之家  ›  专栏  ›  技术社区  ›  ehdv

Visual Studio 2010链接器查找多个定义的符号(不应该的位置)

  •  1
  • ehdv  · 技术社区  · 16 年前

    LNK2005 像这样的错误:

    error LNK2005: "public: double __thiscall Point::GetX(void)const " (?GetX@Point@@QBENXZ) already defined in CppSandbox.obj

    Point.obj )

    最后一个错误是:

    error LNK1169: one or more multiply defined symbols found

    CppSandbox.exe . 我不确定是什么原因导致这个错误-它似乎发生在我建立或重建解决方案。。。说实话,我真的很茫然。

    下面的三个文件是我添加到默认VS2010 blank项目中的全部文件(它们是完整复制的)。谢谢你的帮助。

    h点

    class Point
    {
    public:
        Point() 
        { 
            x = 0; 
            y = 0; 
        };
        Point(double xv, double yv) 
        { 
            x = xv;
            y = yv; 
        };
    
        double GetX() const;
        void SetX(double nval);
    
        double GetY() const;
        void SetY(double nval);
    
        bool operator==(const Point &other)
        {
            return GetX() == other.GetX() && GetY() == other.GetY();
        }
    
        bool operator!=(const Point &other)
        {
            return !(*this == other);
        }
    
    private:
        double x;
        double y;
    };
    

    点.cpp

    #include "Point.h"
    
    double Point::GetX() const
    {
        return x;
    }
    
    double Point::GetY() const
    {
        return y;
    }
    
    void Point::SetX(double nv)
    {
        x = nv;
    }
    
    void Point::SetY(double nv)
    {
        y = nv;
    }
    

    // CppSandbox.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    #include "Point.cpp"
    
    int main()
    {
        Point p(1, 2);
        Point q(1, 2);
        Point r(2, 3);
    
        if (p == q) std::cout << "P == Q";
        if (q == p) std::cout << "Equality is commutative";
        if (p == r || q == r) std::cout << "Equality is broken";
    
        return 0;
    }
    
    1 回复  |  直到 16 年前
        1
  •  8
  •   James McNellis    16 年前

    问题出在 cpp沙盒.cpp

    #include "Point.cpp"
    

    您将包含cpp文件而不是头文件,因此它的内容将编译两次,并且其中的所有内容都将被定义两次(当 点.cpp cpp沙盒.cpp

    您应该将头文件包含在 cpp沙盒.cpp :

    #include "Point.h"
    

    include guards .