mutojack 2018-03-17 01:54 采纳率: 100%
浏览 8058
已采纳

c++ 编译时报错 "undefined reference to ……"

编译情况

总共有五个文件,Graph.h ,Graph.cpp,diamond.h.diamond.cpp,new.cpp
编译成功的是我把diamond类的声明和定义(Graph的声明和定义也放在了一个文件里,但是没有影响)都放在diamond.cpp里。
new.cpp 就是包含了"Graph.h"和"diamond.",然后用diamond创建了一个对象,调用ShapePrint()函数。

Graph.h

 #ifndef _Graph_H_
#define _Graph_H_

class shape
{
    public:
        void ShapePrint();
        void ShapePrint(int x);
        void ShapePrint(int x,int y);
        int GetLength_x();
        int GetLength_y();
    protected:
        void SetLength_x(int length_x);
        void SetLength_y(int length_y);
    private:
        int length_x ;
        int length_y ;

};

#endif

diamond.h

 #include <iostream>
#include <iomanip>
#include "Graph.h"
#ifndef _diamond_
#define _diamond_
class diamond:public shape{
public:
    diamond();

    diamond(int x);

    int GetLength();
protected:
    void SetLength(int l);

private:
    int length;

};
#endif

diamond.cpp

 #include "diamond.h"
using namespace std;
void diamond::SetLength(int l)
{
    this->length = l;
}


int diamond::GetLength()
{
    return length;
}


diamond::diamond()
    {
        SetLength(7);
    }
diamond::diamond(int l)
    {
        SetLength(l);
    }
    void diamond::ShapePrint()
    {
        int length=GetLength();
        for(int i=1;i<=length;++i)
        {
            cout<<setw(length-i+1)<<"*";
            if(i!=1)
                cout<<setw(2*i-2)<<"*"<<endl;
            else
                cout<<endl;
        }
        for(int k=1;k<=length-1;k++)
        {
            cout<<setw(k+1)<<"*";
            if(k==length-1)
                cout<<endl;
            else
                cout<<setw(2*(length-k)-2)<<"*"<<endl;
        }
    }
    void diamond::ShapePrint(int x)
    {
        int length=GetLength();
        for(int i=1;i<=length;++i)
        {
            cout<<setw(x+(length-i+1))<<"*";
            if(i!=1)
                cout<<setw(2*i-2)<<"*"<<endl;
            else
                cout<<endl;
        }
        for(int k=1;k<=length-1;k++)
        {
            cout<<setw(x+k+1)<<"*";
            if(k==length-1)
                cout<<endl;
            else
                cout<<setw(2*(length-k)-2)<<"*"<<endl;
        }
    }
    void diamond::ShapePrint(int x,int y)
    {
        int length=GetLength();
        for(int i=1;i<=y;i++)
            cout<<endl;
        for(int i=1;i<=length;++i)
        {
            cout<<setw(x+(length-i+1))<<"*";
            if(i!=1)
                cout<<setw(2*i-2)<<"*"<<endl;
            else
                cout<<endl;
        }
        for(int k=1;k<=length-1;k++)
        {
            cout<<setw(x+k+1)<<"*";
            if(k==length-1)
                cout<<endl;
            else
                cout<<setw(2*(length-k)-2)<<"*"<<endl;
        }
    }
  • 写回答

4条回答 默认 最新

  • nbits.y 2018-03-17 06:18
    关注

    g++ -o new Graph.cpp diamond.cpp new.cpp 应该没有问题
    你需要了解g++的编译选项怎么用

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。