在c ++中构建错误,未定义的符号,在这里尝试了其他答案,无法弄明白

时间:2016-01-19 03:43:59

标签: c++ build

我的代码编译得很好,但我在构建时遇到此错误。 我明白有些东西与符号defs不相符,但是我已经一遍又一遍地从我看到的,它们都被正确定义并且有正确的参考所以我想也许这是我的问题和实施在一起,但idk。请帮助,请不要因为问题已经被关闭而关闭它,我没有得到其他问题的其他答案的任何帮助。

public override bool Equals(object obj)
{
  return obj is TheStructType && Equals((TheStructType)obj);
}

这是头文件:

/bin/sh -c '/usr/bin/make -j4 -e -f  Makefile'
----------Building project:[ AreaComp - Debug ]----------
/usr/bin/g++ -o ./Debug/AreaComp @"AreaComp.txt" -L.
Undefined symbols for architecture x86_64:
"compute::Area::compSq()", referenced from:
  _main in Areamain.cpp.o
"compute::Area::compHex()", referenced from:
  _main in Areamain.cpp.o
"compute::Area::compOct()", referenced from:
  _main in Areamain.cpp.o
"compute::Area::compTri()", referenced from:
  _main in Areamain.cpp.o
"compute::Area::compHept()", referenced from:
  _main in Areamain.cpp.o
"compute::Area::compPent()", referenced from:
  _main in Areamain.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see    invocation)
make[1]: *** [Debug/AreaComp] Error 1
make: *** [All] Error 2
====1 errors, 0 warnings====

这是实现(尝试.cxx和.cpp ....没有变化):

 #ifndef MAIN_SAVITCH_COMPUTE_H
 #define MAIN_SAVITCH_COMPUTE_H

namespace compute
{
     class Area
     {
     public:

         void compTri();
         void compSq();
         void compPent();
         void compHex();
         void compHept();
         void compOct();

     private:

         double area;

     };

 }
 #endif

这是主文件:

 #include <iostream>
 #include <cmath>
 #include <cassert>
 #include "Area.h"



namespace compute
{

double i;

void Area::compTri()
{
    for(i = 1.00; i < 6.00; i++)
    {
        area = (1.00/2.00)i*((pow((-(1/2)i), 2.00)+pow(i,2.00)));

        cout<< "area for triangle of side length"<< i << "=" << area << endl;
    }
}
void Area::compSq()
{
    for(i = 1.00; i < 6.00; i++)
    {
        area = 2.00*i;

        cout << "area for square of side length" << i << "=" << area << endl;
    }
}
void Area::compPent()
{
    for(i = 1.00; i < 6.00; i++)
    {
        area = (1.00/4.00)*sqrt(5.00*(5.00 + 2.00*sqrt(5.00)))*pow(i,2.00);

        cout << "area for pentagon of side length" << i << "=" << area << endl;
    }
}
void Area::compHex()
{
    for(i = 1.00; i < 6.00; i++)
    {
        area = ((3.00*sqrt(3.00))/2.00)*pow(i,2.00);

        cout << "area for hexagon of side length" << i << "=" << area << endl;
    }
}
void Area::compHept()
{
    for(i = 1.00; i < 6.00; i++)
    {
        area = (7.00/4.00)*pow(i,2.00)*(1.00/tan(180.00/7.00));

        cout << "area for heptagon of side length" << i << "=" << area << endl;
    }
}
void Area::compOct()
{
    for(i = 1.00; i < 6.00; i++)
    {
        area = 2.00*(1.00+sqrt(2.00))*pow(i,2.00);

        cout << "area for octagon of side length" << i << "=" << area << endl;
    }
  }
}

制作档案:

 #include <iostream>
 #include <cassert>
 #include <cmath>
 #include "Area.h"

 using namespace std;
 using namespace compute;

int main(int argc, char **argv)
{

int a;

cout <<"this program will compute the area of a polygon with between 3 & 8"<< endl;


cout << "how many sides does your polygon have?" << endl;


cin >> a;


assert(a >= 3 && a <= 8);


Area find;



if(a == 3)
{
    find.compTri();
}
else if(a == 4)
{
    find.compSq();
}
else if(a == 5)
{
    find.compPent();
}
else if(a == 6)
{
    find.compHex();
}
else if(a == 7)
{
    find.compHept();
}
else
{
    find.compOct();
}

return 0;
};

1 个答案:

答案 0 :(得分:0)

问题出在你的makefile中。您没有与实施相关联。如果您发布Makefile,我可以进一步提供帮助。

正确的解决方案: How to link multiple implementation files in C

其他替代方案:

  • 在主文件中包含实现.cpp文件(不推荐它,但它可以正常工作)
  • 将实现.cpp文件剪切/粘贴到主文件中(相当于上面,但不可扩展)