【问题标题】:include graphic interface on program在程序中包含图形界面
【发布时间】:2014-06-05 21:23:59
【问题描述】:

就像标题说的那样,我认为我的一个包含问题。

我是学生,老师给了我一个解决方案,创建一个可视化界面(由他自己编码),命名为EZ-Draw。

但是在我的代码的某个地方有一个问题,因为我的编译器告诉我这种风格的许多错误:

|364|undefined reference to `CreateCompatibleDC@4'|
|559|undefined reference to `SelectObject@8'|
...

我的代码:

interpreteur.hpp

#ifndef INTERPRETEUR_HPP_INCLUDED
#define INTERPRETEUR_HPP_INCLUDED
#include <sstream>
#include <map>
#include <math.h>
#include "Pile_Template.hpp"
#include "ez-draw++.h"

#define PI 3.14159265

class Interpreteur {
private:
    void (Interpreteur::*ptr)();
    EZWindow myWindow;
    Pile<double> pile;
    Pile<string> pilestr;
    bool run;
public:
    map<string,void(Interpreteur::*)()> myMap;
    Interpreteur();
    ~Interpreteur();
    inline bool getRun() {return run;};
    inline void setEmpilerPile(double nombre) {pile.empiler(nombre);};
    template <typename T>
        string itos(T nombre) // convertit un double en string
        {
            ostringstream ss;
            ss<<nombre;
            return ss.str();
        }
    void addition();
    void moins();
    void multiplie();
    void divise();
    void quit();
    void push();
    void pushstr();
    void pop();
    void popstr();
    void copy();
    void copystr();
    void print();
    void printstr();
    void display();
    void displaystr();
    void count();
    void countstr();
    void swap();
    void swapstr();
    void sinus();
    void cosinus();
    void tangente();
    void racine();
    void trunc();
    void line();
    void color();
    void drawstr();
    void triangle();
    void rectangle();
    void circle();

};
#endif // SOUS_PROGRAMMES_HPP_INCLUDED

interpreteur.cpp

#include "interpreteur.hpp"
#include <sstream>
#include <map>
#include <math.h>
#include <string>

using namespace std;


void Interpreteur::addition()
{
    pile.empiler(pile.depiler()+pile.depiler());
}

void Interpreteur::moins()
{
    double nombre=pile.depiler();
    nombre=pile.depiler()-nombre;
    pile.empiler(nombre);
}

void Interpreteur::multiplie()
{
    pile.empiler(pile.depiler()*pile.depiler());
}

void Interpreteur::divise()
{
    double nombre=pile.depiler();
    nombre=pile.depiler()/nombre;
    pile.empiler(nombre);
}

void Interpreteur::quit()
{
    run=false;
}

void Interpreteur::push()
{
    double i;
    cin>>i;
    pile.empiler(i);
}

void Interpreteur::pushstr()
{
    string chaine;
    char merde;
    cin>>merde;
    if(merde=='"')
    {
        getline(cin,chaine,'"');
        pilestr.empiler(chaine);
    }
    else
    {
        cin.putback(merde);
        cerr<<"mauvaise chaine de caractères"<<endl;
    }
}

void Interpreteur::pop()
{
    pile.depiler();
}

void Interpreteur::popstr()
{
    pilestr.depiler();
}

 void Interpreteur::copy()
{
   int i=pile.depiler();
    pile.empiler(pile[pile.getSommet()-i]);
}

void Interpreteur::copystr()
{
    int i=pile.depiler();
    pilestr.empiler(pilestr[pile.getSommet()-i]);
}

void Interpreteur::print()
{
    cout<<pile.depiler()<<endl;
}

void Interpreteur::printstr()
{
    cout<<pilestr.depiler()<<endl;
}

void Interpreteur::display()
{
    pile.afficher(cout);
}

void Interpreteur::displaystr()
{
    pilestr.afficher(cout);
}

void Interpreteur::count()
{
    pile.empiler(pile.getSommet());
}

void Interpreteur::countstr()
{
    pilestr.empiler(itos(pilestr.getSommet()));
}

void Interpreteur::swap()
{
    double first=pile.depiler();
    double second=pile.depiler();
    pile.empiler(first);
    pile.empiler(second);
}

void Interpreteur::swapstr()
{
    string first=pilestr.depiler();
    string second=pilestr.depiler();
    pilestr.empiler(first);
    pilestr.empiler(second);
}

void Interpreteur::sinus()
{
    pile.empiler(sin(pile.depiler()*PI/180));
}

void Interpreteur::cosinus()
{
    pile.empiler(cos(pile.depiler()*PI/180));
}

void Interpreteur::tangente()
{
    pile.empiler(tan(pile.depiler()*PI/180));
}

void Interpreteur::racine()
{
    pile.empiler(sqrt(pile.depiler()));
}

void Interpreteur::trunc()
{
    int x=pile.depiler();
    pile.empiler(x);
}

void Interpreteur::line()
{
    int y2=pile.depiler();
    int x2=pile.depiler();
    int y1=pile.depiler();
    int x1=pile.depiler();
    myWindow.drawLine(x1,y1,x2,y2);
}

void Interpreteur::color()
{
    int couleur=pile.depiler();
    switch(couleur)
    {
        case 1:{myWindow.setColor(ez_black);break;}
        case 2:{myWindow.setColor(ez_red);break;}
        case 3:{myWindow.setColor(ez_green);break;}
        case 4:{myWindow.setColor(ez_blue);break;}
        case 5:{myWindow.setColor(ez_cyan);break;}
        case 6:{myWindow.setColor(ez_magenta);break;}
        case 7:{myWindow.setColor(ez_yellow);break;}
        //pourquoi que on a pas fait le gris ? ez_grey
        default:{pile.empiler(couleur); cerr<<"couleur inconnue"<<endl; break;}
    }
    // COULEUR : ez_black, ez_white, ez_grey, ez_red, ez_green, ez_blue,ez_yellow, ez_cyan, ez_magenta

}

void Interpreteur::drawstr()
{
    string str=pilestr.depiler();
    int y1=pile.depiler();
    int x1=pile.depiler();
    myWindow.drawText(EZ_MC,x1,y1,str);
}

void Interpreteur::triangle()
{
    int y3=pile.depiler();
    int x3=pile.depiler();
    int y2=pile.depiler();
    int x2=pile.depiler();
    int y1=pile.depiler();
    int x1=pile.depiler();
    myWindow.drawTriangle(x1,y1,x2,y2,x3,y3);
}

void Interpreteur::rectangle()
{
    int y2=pile.depiler();
    int x2=pile.depiler();
    int y1=pile.depiler();
    int x1=pile.depiler();
    myWindow.drawRectangle(x1,y1,x2,y2);
}

void Interpreteur::circle()
{
    int y2=pile.depiler();
    int x2=pile.depiler();
    int y1=pile.depiler();
    int x1=pile.depiler();
    myWindow.drawCircle(x1,y1,x2,y2);
}

Interpreteur::Interpreteur()
{
    run=true;
    myMap["+"]=&Interpreteur::addition;
    myMap["-"]=&Interpreteur::moins;
    myMap["*"]=&Interpreteur::multiplie;
    myMap["/"]=&Interpreteur::divise;
    myMap["exit"]=&Interpreteur::quit;
    myMap["push"]=&Interpreteur::push;
    myMap["pushstr"]=&Interpreteur::pushstr;
    myMap["pop"]=&Interpreteur::pop;
    myMap["popstr"]=&Interpreteur::popstr;
    myMap["copy"]=&Interpreteur::copy;
    myMap["copystr"]=&Interpreteur::copystr;
    myMap["print"]=&Interpreteur::print;
    myMap["printstr"]=&Interpreteur::printstr;
    myMap["display"]=&Interpreteur::display;
    myMap["displaystr"]=&Interpreteur::displaystr;
    myMap["count"]=&Interpreteur::count;
    myMap["countstr"]=&Interpreteur::countstr;
    myMap["swap"]=&Interpreteur::swap;
    myMap["swapstr"]=&Interpreteur::swapstr;
    myMap["sin"]=&Interpreteur::sinus;
    myMap["cos"]=&Interpreteur::cosinus;
    myMap["tan"]=&Interpreteur::tangente;
    myMap["sqrt"]=&Interpreteur::racine;
    myMap["trunc"]=&Interpreteur::trunc;
    myMap["line"]=&Interpreteur::line;
    myMap["color"]=&Interpreteur::color;
    myMap["drawstr"]=&Interpreteur::drawstr;
    myMap["triangle"]=&Interpreteur::triangle;
    myMap["rectangle"]=&Interpreteur::rectangle;
    myMap["circle"]=&Interpreteur::circle;
}

Interpreteur::~Interpreteur()
{
    map<string, void (Interpreteur::*)()>::iterator it;
    myMap.erase(it);
}

这里是我老师给的"EZ-Draw documentation",用于理解ez-draw和ez-draw++

我不明白编译器试图告诉我什么

【问题讨论】:

  • 您可能需要编辑您的帐户说明,法语中的“étudiant”在英语中拼写为“学生”。

标签: c++ graphics compiler-errors include


【解决方案1】:

您正在使用 C++ 中的 C 函数,为此您需要明确告诉编译器这些是 C 函数。您在 C++ 中使用的 C 头文件应包含在 C++ 中可用的那些行:

#ifdef __cplusplus
extern "C" {
#endif

//The header file which declares the functions which are not linked correctly here

#ifdef __cplusplus
}
#endif

一点解释:
C 和 C++ 的函数“名称修饰”是不同的,因为在 C++ 中,您可以重载一个函数,单独的函数名称并不能唯一标识一个函数,因此编译器在编译时会在后台的函数名称中添加一些符号使这些名称独一无二(即CreateCompatibleDC@4 中的DC@4)。

由于您的链接器需要 C++ 函数,因此它会搜索 CreateCompatibleDC@4,但您的一些文件会在 C 中编译,并导出一个名为 CreateCompatible 的函数,这就是您得到“未定义引用”的原因:链接器告诉你它找不到某些函数的定义。

【讨论】:

  • 好的,但是我把你刚刚写的代码 sn-p 放在哪里:#ifdef __cplusplus extern "C" { ... } #endif
  • 在头文件的开头和结尾声明了给你错误信息的函数。
猜你喜欢
  • 1970-01-01
  • 2014-03-17
  • 1970-01-01
  • 2014-08-05
  • 1970-01-01
  • 1970-01-01
  • 2010-10-11
  • 1970-01-01
  • 2011-09-06
相关资源
最近更新 更多