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 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办