linkage problems

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Post Reply
Domi104
Posts: 2
Joined: Sun Nov 22, 2020 9:34 am

linkage problems

Post by Domi104 »

Good evening,

I have made a C++ Builder project able to display tree kind of curves but the problem is that I want to run this project with two sources by calling in an one file some extern global parameters like functions, types of datas, pointers of class, etc... In one project, that is running perfectly but my curves are displayed in the whole form and not in a TPaintBox control. Thank you for your help!

Code: Select all

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit21.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TGraphe *Graphe;
//---------------------------------------------------------------------------
__fastcall TGraphe::TGraphe(TComponent* Owner)
	: TForm(Owner)
{
}

//__fastcall TGraph::~TGraph()

//{
//}
//---------------------------------------------------------------------------
void __fastcall TGraphe::Repere_cartesien(TObject *Sender)
{

TPaintBox *Graph;

// Définition des variables relatives à l'axe des ordonnées


int axe_ordonnees;
int axe_ord2;
int axe_ord3;
int axe_ord4;
int axe_ord5;
int axe_ord6;
int axe_ord7;

// Définition des variables relatives à l'axe des abscisses

int axe_abscisses;
int axe_abs2;
int axe_abs3;
int axe_abs4;
int axe_abs5;
int axe_abs6;
int axe_abs7;
//
//Création d'un repère orthonormé
//
axe_ordonnees=Graph->Width/2;
axe_ord2=Graph->Width/4;
axe_ord3=Graph->Width*0.75;
axe_ord4=Graph->Width*0.125;
axe_ord5=Graph->Width*0.375;
axe_ord6=Graph->Width*0.625;
axe_ord7=Graph->Width*0.875;

axe_abscisses=Graph->Height/2;
axe_abs2=Graph->Height/4;
axe_abs3=Graph->Height*0.75;
axe_abs4=Graph->Height*0.125;
axe_abs5=Graph->Height*0.375;
axe_abs6=Graph->Height*0.625;
axe_abs7=Graph->Height*0.875;

Graph->Canvas->Brush->Color=clBlack;
Graph->Canvas->Pen->Color=clGray;
Graph->Canvas->MoveTo(0,axe_abscisses);
Graph->Canvas->LineTo(Graph->Width,axe_abscisses);
Graph->Canvas->MoveTo(axe_ordonnees,0);
Graph->Canvas->LineTo(axe_ordonnees,Graph->Height);
Graph->Canvas->MoveTo(axe_ord2,0);
Graph->Canvas->LineTo(axe_ord2,Graph->Height);
Graph->Canvas->MoveTo(axe_ord3,0);
Graph->Canvas->LineTo(axe_ord3,Graph->Height);
Graph->Canvas->MoveTo(axe_ord4,0);
Graph->Canvas->LineTo(axe_ord4,Graph->Height);
Graph->Canvas->MoveTo(axe_ord5,0);
Graph->Canvas->LineTo(axe_ord5,Graph->Height);
Graph->Canvas->MoveTo(axe_ord6,0);
Graph->Canvas->LineTo(axe_ord6,Graph->Height);
Graph->Canvas->MoveTo(axe_ord7,0);
Graph->Canvas->LineTo(axe_ord7,Graph->Height);

Graph->Canvas->MoveTo(0,axe_abs2);
Graph->Canvas->LineTo(Graph->Width,axe_abs2);
Graph->Canvas->MoveTo(0,axe_abs3);
Graph->Canvas->LineTo(Graph->Width,axe_abs3);
Graph->Canvas->MoveTo(0,axe_abs4);
Graph->Canvas->LineTo(Graph->Width,axe_abs4);
Graph->Canvas->MoveTo(0,axe_abs5);
Graph->Canvas->LineTo(Graph->Width,axe_abs5);
Graph->Canvas->MoveTo(0,axe_abs6);
Graph->Canvas->LineTo(Graph->Width,axe_abs6);
Graph->Canvas->MoveTo(0,axe_abs7);
Graph->Canvas->LineTo(Graph->Width,axe_abs7);

// Fin de création du repère.

//Dessin d'un repère cartésien

}

//---------------------------------------------------------------------------

#ifndef Unit21H
#define Unit21H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TGraphe : public TForm
{
__published:	// Composants gérés par l'EDI
	TButton *Button1;
	void __fastcall FormCreate(TObject *Sender);
	void __fastcall Repere_cartesien(TObject *Sender);
private:	// Déclarations utilisateur
public:		// Déclarations utilisateur
//	__fastcall ~Graphe);
	__fastcall Graphe(TComponent *Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TGraphe *Graphe;
//---------------------------------------------------------------------------
#endif

//---------------------------------------------------------------------------
 //Auteur: Delteil Dominique - ACONIT

//Sujet: mini générateur de fonctions
//triangle, sinus, cosinus

//---------------------------------------------------------------------------

#include <vcl.h>
#include <stdlib.h>
#pragma hdrstop

#include "Unit21.h"
#include "Unit22.h"
#include <math.h>

#define pi 3.141572
#define Pi 6.283185

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "Unit21.dfm"

// implémentation de la classe TGraphique

extern TPaintBox *Graph;

extern void Repere_cartesien(TObject *Sender);
//extern TPaintBox *PaintBox;


//extern TPaintBox *PaintBox;

Tproj	*proj;

extern TCanvas *Canvas;

int axe_abscisses;



//---------------------------------------------------------------------------
__fastcall Tproj::Tproj(TComponent* Owner): TForm(Owner)

{

}
//  Définition d'un destructeur afin de libérer la mémoire
// lors de la fermeture du programme

__fastcall Tproj::~Tproj()

{

// Destruction de l'objet Graphique


}

//Création d'un repère cartésien



void __fastcall Tproj::calculs(TObject *Sender)

{

double s,Temps,Angle,Periode,Amplitude=100;

Graph->Canvas->Pen->Color=clBlue;

Temps=0.02;
Angle=3;
Periode=0;

s=proj->Sinus(Angle,Temps,Periode)*Amplitude;
Graph->Canvas->MoveTo(0,axe_abscisses-s);

// Tracé de ma courbe sinusoïdale

for(int j=0;j<Graph->Width;j++)
{
s=proj->Sinus(Angle,Temps,Periode)*Amplitude;
Graph->Canvas->LineTo(j,axe_abscisses-s);
Periode=Periode+0.01;

}

// Tracé de la courbe triangle

// Définition de la couleur de la courbe triangulaire

Graph->Canvas->Pen->Color=clGreen;

double Sa=0;
double Fr=500;

s=proj->Triangle(Sa,Fr)*Amplitude;
Graph->Canvas->MoveTo(0,axe_abscisses-s);

for(int j=0;j<Graph->Width;j++)
{
s=proj->Triangle(Sa,Fr)*Amplitude;
Graph->Canvas->LineTo(j,axe_abscisses-s);
Sa++;
}

// Tracé de ma courbe cosinusoïdale

// Définition de la couleur de la courbe cosinus

Graph->Canvas->Pen->Color=clRed;
Temps=0.02;
Angle=3;
Periode=0;
s=proj->Cosinus(Angle,Temps,Periode)*Amplitude;
Graph->Canvas->MoveTo(0,axe_abscisses-s);


for(int j=0;j<Graph->Width;j++)
{

s=proj->Cosinus(Angle,Temps,Periode)*Amplitude;
Graph->Canvas->LineTo(j,axe_abscisses-s);
Periode=Periode+0.01;

}

}

//Définition de la courbe sinus par rapport à une vitesse angulaire et une période

double __fastcall Tproj::Sinus(double a,double t, double T)
{
return sin((a*pi/t)+ T);
}

// idem que pour la définition de la courbe cosinus

double __fastcall Tproj::Cosinus(double a,double t, double T)
{
return cos((a*pi/t)+ T);
}

double __fastcall Tproj::Triangle(double S, double F)
{
double d=fmod(S*F*Pi/44100,Pi);
d=(pi-d)/pi;
if(d<=0.0)
d=-d;
return(1.0-(d*2.0));
}

// Définition de l'action par rapport à l'évènement OnClick sur le formulaire

//---------------------------------------------------------------------------


void __fastcall Tproj::BoutonClick(TObject *Sender)
{
Repere_cartesien(Bouton);
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------

#ifndef Unit22H
#define Unit22H
//---------------------------------------------------------------------------

#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.ExtCtrls.hpp>
//---------------------------------------------------------------------------
class Tproj : public TForm
{
__published:	// Composants gérés par l'EDI

	TButton *Bouton;
private:	// Déclarations utilisateur
public:		// Déclarations utilisateur
	__fastcall Tproj(TComponent* Owner);
	__fastcall ~Tproj();
	void __fastcall BoutonClick(TObject *Sender);
	double __fastcall Triangle(double S, double F);
	double __fastcall Cosinus(double a,double t, double T);
	double __fastcall Sinus(double a,double t, double T);
	void __fastcall calculs(TObject *Sender);
};
//---------------------------------------------------------------------------
extern PACKAGE Tproj *proj;
//---------------------------------------------------------------------------
#endif

But it can't run so I have made an other program which runs...


//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
 //Auteur: Delteil Dominique - ACONIT

//Sujet: mini générateur de fonctions
//triangle, sinus, cosinus



//---------------------------------------------------------------------------

#include <vcl.h>

// en-tête à supprimer éventuellement

#include <stdlib.h>
#pragma hdrstop

#include "Unit14.h"

#include "Unit15.h"
#include <math.h>

#define pi 3.141572
#define Pi 6.283185

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

// implémentation de la classe TGraphique

extern  curves *Courbes;

extern	double __fastcall Triangle(double S, double F);
extern	double __fastcall Sinus(double a, double t, double T);
extern	double __fastcall Cosinus(double a,double t, double T);

TGraphique *Graphique;

TCanvas *Canvas;

int axe_abscisses;

double Amplitude=100;

//---------------------------------------------------------------------------
__fastcall TGraphique::TGraphique(TComponent* Owner): TForm(Owner)

{

}
//

__fastcall TGraphique::~TGraphique()

{

delete Graphique;

}

void __fastcall TGraphique::repere_cartesien()

{

int axe_ordonnees;
int axe_ord2;
int axe_ord3;
int axe_ord4;
int axe_ord5;
int axe_ord6;
int axe_ord7;

int axe_abs2;
int axe_abs3;
int axe_abs4;
int axe_abs5;
int axe_abs6;
int axe_abs7;
//
//Création d'un repère orthonormé
//
axe_ordonnees=Graphique->ClientWidth/2;
axe_ord2=Graphique->ClientWidth/4;
axe_ord3=Graphique->ClientWidth*0.75;
axe_ord4=Graphique->ClientWidth*0.125;
axe_ord5=Graphique->ClientWidth*0.375;
axe_ord6=Graphique->ClientWidth*0.625;
axe_ord7=Graphique->ClientWidth*0.875;

axe_abscisses=Graphique->ClientHeight/2;
axe_abs2=Graphique->ClientHeight/4;
axe_abs3=Graphique->ClientHeight*0.75;
axe_abs4=Graphique->ClientHeight*0.125;
axe_abs5=Graphique->ClientHeight*0.375;
axe_abs6=Graphique->ClientHeight*0.625;
axe_abs7=Graphique->ClientHeight*0.875;

Canvas->Brush->Color=clBlack;
Canvas->Pen->Color=clGray;
Canvas->MoveTo(0,axe_abscisses);
Canvas->LineTo(Graphique->Width,axe_abscisses);
Canvas->MoveTo(axe_ordonnees,0);
Canvas->LineTo(axe_ordonnees,Graphique->Height);
Canvas->MoveTo(axe_ord2,0);
Canvas->LineTo(axe_ord2,Graphique->Height);
Canvas->MoveTo(axe_ord3,0);
Canvas->LineTo(axe_ord3,Graphique->Height);
Canvas->MoveTo(axe_ord4,0);
Canvas->LineTo(axe_ord4,Graphique->Height);
Canvas->MoveTo(axe_ord5,0);
Canvas->LineTo(axe_ord5,Graphique->Height);
Canvas->MoveTo(axe_ord6,0);
Canvas->LineTo(axe_ord6,Graphique->Height);
Canvas->MoveTo(axe_ord7,0);
Canvas->LineTo(axe_ord7,Graphique->Height);

Canvas->MoveTo(0,axe_abs2);
Canvas->LineTo(Graphique->Width,axe_abs2);
Canvas->MoveTo(0,axe_abs3);
Canvas->LineTo(Graphique->Width,axe_abs3);
Canvas->MoveTo(0,axe_abs4);
Canvas->LineTo(Graphique->Width,axe_abs4);
Canvas->MoveTo(0,axe_abs5);
Canvas->LineTo(Graphique->Width,axe_abs5);
Canvas->MoveTo(0,axe_abs6);
Canvas->LineTo(Graphique->Width,axe_abs6);
Canvas->MoveTo(0,axe_abs7);
Canvas->LineTo(Graphique->Width,axe_abs7);

// Fin de création du repère.

}

//---------------------------------------------------------------------------
void __fastcall TGraphique::Dessine()
{

// Déclaration des types de données relatives aux calculs des courbes Sinus et Cosinus


double s,Temps,Angle,Periode;

//Dessin d'un repère cartésien

Graphique->repere_cartesien();

// Définition de la couleur de la courbe sinus

Canvas->Pen->Color=clBlue;

Temps=0.02;
Angle=3;
Periode=0;

s=Courbes->Sinus(Angle,Temps,Periode)*Amplitude;
Canvas->MoveTo(0,axe_abscisses-s);

// Tracé de ma courbe sinusoïdale

for(int j=0;j<Graphique->Width;j++)
{
s=Courbes->Sinus(Angle,Temps,Periode)*Amplitude;
Canvas->LineTo(j,axe_abscisses-s);
Periode=Periode+0.01;

}

// Tracé de la courbe triangle

// Définition de la couleur de la courbe triangulaire

Canvas->Pen->Color=clGreen;

double Sa=0;
double Fr=500;

s=Courbes->Triangle(Sa,Fr)*Amplitude;
Canvas->MoveTo(0,axe_abscisses-s);

for(int j=0;j<Graphique->Width;j++)
{
s=Courbes->Triangle(Sa,Fr)*Amplitude;
Canvas->LineTo(j,axe_abscisses-s);
Sa++;
}

// Tracé de ma courbe cosinusoïdale

// Définition de la couleur de la courbe cosinus

Canvas->Pen->Color=clRed;
Temps=0.02;
Angle=3;
Periode=0;
s=Courbes->Cosinus(Angle,Temps,Periode)*Amplitude;
Canvas->MoveTo(0,axe_abscisses-s);


for(int j=0;j<Graphique->Width;j++)
{

s=Courbes->Cosinus(Angle,Temps,Periode)*Amplitude;
Canvas->LineTo(j,axe_abscisses-s);
Periode=Periode+0.01;

}

}



// Définition de l'action par rapport à l'évènement OnClick sur le formulaire


void __fastcall TGraphique::boutonClick(TObject *Sender)
{
 Graphique->Dessine();
}
//---------------------------------------------------------------------------

//---------------------------------------------------------

#ifndef Unit14H
#define Unit14H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.ExtCtrls.hpp>

//---------------------------------------------------------------------------
class TGraphique : public TForm
{
__published:	// Composants gérés par l'EDI;
	TPaintBox *PaintBox1;
	TButton *bouton;

// possible erreur de code
//ligne du dessous
//void __fastcall Touche_FrequenceClick(TObject *Sender);
	void __fastcall boutonClick(TObject *Sender);

private:	// Déclarations utilisateur
public:		// Déclarations utilisateur
	__fastcall ~TGraphique();
	void __fastcall Dessine();

	__fastcall TGraphique(TComponent* Owner);
	void __fastcall repere_cartesien();

};
//---------------------------------------------------------------------------
extern PACKAGE TGraphique *Graphique;
//---------------------------------------------------------------------------
#endif

//---------------------------------------------------------------------------

#pragma hdrstop

#include "Unit14.h"
#include "Unit15.h"

#define pi 3.14

#define Pi 6.28
//---------------------------------------------------------------------------
#pragma package(smart_init)

curves *Courbes;

__fastcall curves::curves(TComponent* Owner)

{

}
//

__fastcall curves::~curves()

{

delete Courbes;

}

//Définition de la courbe sinus par rapport à une vitesse angulaire et une période

double __fastcall curves::Sinus(double a,double t, double T)
{
return sin((a*pi/t)+ T);
}

// idem que pour la définition de la courbe cosinus

double __fastcall curves::Cosinus(double a,double t, double T)
{
return cos((a*pi/t)+ T);
}

double __fastcall curves::Triangle(double S, double F)
{
double d=fmod(S*F*Pi/44100,Pi);
d=(pi-d)/pi;
if(d<=0.0)
d=-d;
return(1.0-(d*2.0));
}

//---------------------------------------------------------------------------

#ifndef Unit15H
#define Unit15H
//---------------------------------------------------------------------------
//---------------------------------------------------------

//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.ExtCtrls.hpp>

//---------------------------------------------------------------------------
class curves
{
//__published:	// Composants gérés par l'EDI;

// possible erreur de code
//ligne du dessous
//	void __fastcall Touche_FrequenceClick(TObject *Sender);

private:	// Déclarations utilisateur
public:		// Déclarations utilisateur
	__fastcall ~curves();
	double __fastcall Triangle(double S, double F);
	double __fastcall Sinus(double a, double t, double T);
	double __fastcall Cosinus(double a,double t, double T);
	__fastcall curves(TComponent* Owner);

};
//---------------------------------------------------------------------------
//extern PACKAGE curves *Courbes;
//---------------------------------------------------------------------------
#endif
Edited by moderator
rlebeau
BCBJ Author
BCBJ Author
Posts: 1759
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA
Contact:

Re: linkage problems

Post by rlebeau »

Domi104 wrote: Sat Nov 28, 2020 5:22 pm

Code: Select all

void __fastcall TGraphe::Repere_cartesien(TObject *Sender)
{

TPaintBox *Graph;
...
axe_ordonnees=Graph->Width/2;
...
Graph->Canvas->Brush->Color=clBlack;
...
}
I see you declaring a local TPaintBox variable named Graph, but I don't see it being assigned to point at a valid TPaintBox object before you try to access its members. Where is this version of Repere_cartesien() being called from exactly? Is it supposed to be an OnPaint handler? There is no TPaintBox member in your TGraphe form class.

I see you have separate extern declarations for a global variable named Graph and a standalone function named Repere_cartesien(), which have no relation to your TGraph form at all, but are being used by your Tproj form class. But I don't see that Graph variable being assigned to point at anything, either.

Your code seems to be setup all wrong, so it is little wonder why it doesn't work correctly. What are you trying to accomplish with it exactly?
Remy Lebeau (TeamB)
Lebeau Software
Domi104
Posts: 2
Joined: Sun Nov 22, 2020 9:34 am

Re: linkage problems

Post by Domi104 »

Hello Rémy,

I have encountered your message. I really have made some errors. The problem is that I was obliged to copy and paste my C source code...I wanted to know if I could join you my sources files by email. But you are right, there is something wrong with
my project so that it can run perfectly. Thank you very much for your help!

D.Delteil
Post Reply