Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 13
  1. #1
    Utente di HTML.it
    Registrato dal
    May 2007
    Messaggi
    60

    (c++)errori di link con vc++

    salve, ho qui un programma che nn ha errori di compilazione ma vc++ mi segnala errori nel link
    ---------------------------------------------------------------------------------------
    //stack1.h
    #ifndef STACK1_H
    #define STACK1_H
    template <typename T> class Stack{
    public:
    Stack(int=10);//costruttore
    ~Stack();//ditruttore alt+126
    bool Push(T);
    bool Pop(T &);
    bool isEmpty();
    bool isFull();
    void getTOS(int &);
    private:
    int size;
    T *ptr;
    int top;
    };

    #endif

    ---------------------------------------------------------------------------------------
    //stack.cpp
    #include "stack1.h"
    #include <iostream>
    using namespace std;

    //costruttore ello stack
    template <typename T> Stack <T>::Stack(int num):size(num > 0 ? num : 10),ptr(new T[size]),top(-1){}
    //inserimento di un elemento in testa
    template <typename T> bool Stack <T>::Push(T obj){
    if(top<size-1){

    ptr[top]=obj;
    ++top;
    return 1;
    }
    cout<<"Stack pieno.\nNon è possibile inserire nuovi elementi\n";
    return 0;
    }
    //toglie un elemento dalla testa
    template <typename T> bool Stack <T>::Pop(T &obj){
    if(top>-1){
    obj=ptr[top];
    --top;
    return 1;
    }
    cout<<"Stack vuoto.\nImpossibile togliere elementi\n";
    return 0;
    }
    template <typename T> bool Stack <T>::isEmpty(){return(top==-1 ? 1:0);}
    template <typename T> bool Stack <T>::isFull(){return(top==(size-1) ? 1:0)};
    template <typename T> Stack <T>::~Stack(){delete [] ptr;}
    template <typename T> void Stack <T>::getTOS(int &TOS){TOS=top;}
    ---------------------------------------------------------------------------------------
    //main.cpp
    #include "stack1.h"
    #include <iostream>
    using namespace std;

    int main(){
    int siz;
    int obj;
    int TOS;
    cout<<"Immetti la dimensione dello stack...\n";
    cin>>siz;
    Stack <int> intStack(siz);
    cout<<"push 2 interi nello stack...3 2..\n";
    intStack.Push(3);
    intStack.Push(2);
    intStack.getTOS(TOS);
    cout<<"TOS: "<<TOS<<"\n";
    cout<<"pop 1 elemento..\n";
    intStack.Pop(obj);
    cout<<"L'elemento poppato è: "<<obj<<"\n";
    return 0;
    }
    ---------------------------------------------------------------------------------------
    Quando linko ho:

    Compiling...
    main.cpp
    stack.cpp
    Linking...
    main.obj : error LNK2001: unresolved external symbol "public: __thiscall Stack<int>::~Stack<int>(void)" (??1?$Stack@H@@QAE@XZ)
    main.obj : error LNK2001: unresolved external symbol "public: bool __thiscall Stack<int>::Pop(int &)" (?Pop@?$Stack@H@@QAE_NAAH@Z)
    main.obj : error LNK2001: unresolved external symbol "public: void __thiscall Stack<int>::getTOS(int &)" (?getTOS@?$Stack@H@@QAEXAAH@Z)
    main.obj : error LNK2001: unresolved external symbol "public: bool __thiscall Stack<int>::Push(int)" (?Push@?$Stack@H@@QAE_NH@Z)
    main.obj : error LNK2001: unresolved external symbol "public: __thiscall Stack<int>::Stack<int>(int)" (??0?$Stack@H@@QAE@H@Z)
    Debug/stack.exe : fatal error LNK1120: 5 unresolved externals
    Error executing link.exe.

    stack.exe - 6 error(s), 0 warning(s)
    ---------------------------------------------------------------------------------------
    Qualcuno mi puo aiutare?
    Grazie

  2. #2
    Utente di HTML.it L'avatar di KrOW
    Registrato dal
    Feb 2009
    Messaggi
    281
    Ciao . . . Attento che (nel caso di funzioni/metodi template) la dichiarazione e la definizione devono combaciare . . . In poche parole devi trattare queste funzioni/metodi template, come inline
    codice:
    template <class T>
    class Stack
    {
    Stack(int i=10)
    {// qui definisci il metodo
    }

  3. #3
    Utente di HTML.it
    Registrato dal
    May 2007
    Messaggi
    60
    ok ho modificato il sorgente come mi hai detto..
    ------------------------------------------------------------------------------------
    //stack1.h


    #ifndef STACK1_H
    #define STACK1_H
    template <typename T> class Stack{
    public:
    Stack(int=10);//costruttore
    ~Stack();//ditruttore alt+126
    bool Push(T);
    bool Pop(T &);
    bool isEmpty();
    bool isFull();
    void getTOS(int &);
    private:
    int size;
    T *ptr;
    int top;
    };

    //costruttore ello stack
    template <typename T> Stack <T>::Stack(int num):size(num > 0 ? num : 10),ptr(new T[size]),top(-1){}
    //inserimento di un elemento in testa
    template <typename T> bool Stack <T>::Push(T obj){
    if(top<size-1){

    ptr[top]=obj;
    ++top;
    return 1;
    }
    cout<<"Stack pieno.\nNon è possibile inserire nuovi elementi\n";
    return 0;
    }
    //toglie un elemento dalla testa
    template <typename T> bool Stack <T>::Pop(T &obj){

    if(top>-1){
    obj=ptr[top-1];
    top--;
    return 1;
    }
    cout<<"Stack vuoto.\nImpossibile togliere elementi\n";
    return 0;
    }
    template <typename T> bool Stack <T>::isEmpty(){return(top==-1 ? 1:0);}
    template <typename T> bool Stack <T>::isFull(){return(top==(size-1) ? 1:0)};
    template <typename T> Stack <T>::~Stack(){delete [] ptr;}
    template <typename T> void Stack <T>::getTOS(int &TOS){TOS=top;}


    #endif
    --------------------------------------------------------------------------------------
    //main.cpp

    #include "stack1.h"
    #include <iostream>
    using namespace std;

    int main(){
    int siz;
    int obj;
    int TOS;
    cout<<"Immetti la dimensione dello stack...\n";
    cin>>siz;
    Stack <int> intStack(siz);
    cout<<"push 2 interi nello stack...3 2..\n";
    intStack.Push(3);
    intStack.Push(2);
    intStack.getTOS(TOS);
    cout<<"TOS: "<<TOS<<"\n";
    cout<<"pop 1 elemento..\n";
    intStack.Pop(obj);
    cout<<"L'elemento poppato e': "<<obj<<"\n";
    return 0;
    }
    ------------------------------------------------------------------------------------
    ora sembra tutto a posto..però quando eseguo alla fine del prog mi da un errore::

    Debug error!!! Program:Stack.exe
    Damage:before normal block(#70)at 0x00ecc

    cosa ho sbagliato??non riesco a capire

  4. #4
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Attento agli indici. Non è consentito un indice negativo in un array.
    La prima chiamata a push si traduce come:
    codice:
    if(top<size-1){
    
       ptr[-1]=obj;
       ++top;
       return 1;
    }
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  5. #5
    Utente di HTML.it
    Registrato dal
    May 2007
    Messaggi
    60
    grazie..ho risolto..ora ho un altro problema:un programma che nn riesco a compilare..eccolo qua:

    ---------------------------------------------------------------------------------------

    #include<iostream>


    #ifndef POINT_H
    #define POINT_H

    using namespace std;

    class Point{
    friend ostream &operator<<(ostream &,const Point &);
    public:
    Point(int=0,int=0);
    GetX();
    GetY();
    private:
    int x;
    int y;
    };

    #endif
    ----------------------------------------------------------------------------------------
    //point.cpp

    #include <iostream>
    #include <iomanip>

    #include "point.h"
    //costruttore
    Point::Point(int primo,int secondo){
    x=primo;
    y=secondo;
    }

    int Point::GetX(){
    return x;
    }
    int Point::GetY(){
    return y;
    }

    ostream &operator<<(ostream &output,const Point &){
    ostream<<"["<<setw(4)<<Point::GetX()<<","<<setw(4)<<Point::Ge tY()<<"]\n";
    return ostream;
    }
    ----------------------------------------------------------------------------------------
    qua mi da alcuni errori di compliazione che nn mi so spiegare..
    Compiling...
    point.cpp
    c:\documents and settings\emanuele\desktop\array\point.cpp(21) : error C2143: syntax error : missing ';' before '<<'
    c:\documents and settings\emanuele\desktop\array\point.cpp(21) : error C2143: syntax error : missing ';' before '<<'
    c:\documents and settings\emanuele\desktop\array\point.cpp(22) : error C2275: 'ostream' : illegal use of this type as an expression
    c:\programmi\microsoft visual studio\vc98\include\iosfwd(257) : see declaration of 'ostream'

    che devo fare x farlo funzionare??

  6. #6
    Utente di HTML.it L'avatar di KrOW
    Registrato dal
    Feb 2009
    Messaggi
    281
    In point.cpp prova a mettere std:stream al posto di ostream (come valore di ritorno e nei parametri della funzione) mentre dentro la stessa funzione al posto di ostream metti output

  7. #7
    Utente di HTML.it
    Registrato dal
    May 2007
    Messaggi
    60
    grazie ho risolto

  8. #8
    Utente di HTML.it L'avatar di KrOW
    Registrato dal
    Feb 2009
    Messaggi
    281
    Di niente . . . Ciao

  9. #9
    Utente di HTML.it
    Registrato dal
    May 2007
    Messaggi
    60
    scusate ancora una cosa..ho ancora un programmino che nn mi risce di far funzionare:
    --------------------------------------------------------------------------------------
    //point.h
    //////////////////////////////////////////////
    //Ereditarietà:classi punto e cerchio //
    //////////////////////////////////////////////

    #include<iostream>


    #ifndef POINT_H
    #define POINT_H

    using namespace std;

    class Point{
    friend ostream &operator<<(ostream &,const Point &);
    public:
    Point(int=0,int=0);
    GetX();
    GetY();
    private:
    int x;
    int y;
    };

    #endif
    ---------------------------------------------------------------------------------------
    //point.cpp

    #include <iomanip>
    #include <iostream>

    #include "point.h"
    //costruttore
    Point::Point(int primo,int secondo){
    x=primo;
    y=secondo;
    }

    int Point::GetX(){
    return x;
    }
    int Point::GetY(){
    return y;
    }

    ostream &operator<<(ostream &output,Point &p){
    output<<"["<<setw(4)<<p.GetX()<<","<<setw(4)<<p.GetY()<< "]\n";
    return output;
    }
    ----------------------------------------------------------------------------------------
    //circle.h

    #ifndef CIRCLE_H
    #define CIRCLE_H
    #include <iostream>
    #include "point.h"
    //using namespace std;


    class Circle : public Point{
    friend ostream &operator<<(ostream &,Circle &);
    public:
    Circle(double =0.0;int =0,int =0);
    double GetRadius();
    double Area();
    protected:
    double radius;
    };

    #endif
    ----------------------------------------------------------------------------------------
    //circle.cpp
    #include "circle.h"
    #include "point.h"
    #include <iomanip>
    #include <iostream>
    #define PI 3.14
    using namespace std;


    Circle::Circle(double r,int x,int y):
    Point(x,y){
    radius=r;
    }
    double Circle::GetRadius(){
    return radius;
    }
    double Circle::Area(){
    return (radius*radius*PI);
    }
    ostream &operator<<(ostream &output,Circle &c){
    output<<"Il cerchio ha centro: "<<static_cast<Point>(c)<<"\n"
    <<"Il raggio del cechio è: "<<c.GetRadius()<<"\n"
    <<"L'area è: "<<c.Area()<<endl;
    return output;
    }
    ---------------------------------------------------------------------------------------
    la classe Point me la compila senza errori..la classe Circle mi da questi errori:
    ---------------------------------------------------------------------------------------
    Compiling...
    circle.cpp
    c:\documents and settings\emanuele\desktop\array\circle.h(13) : error C2143: syntax error : missing ')' before ';'
    c:\documents and settings\emanuele\desktop\array\circle.h(13) : error C2143: syntax error : missing ')' before ';'
    c:\documents and settings\emanuele\desktop\array\circle.h(13) : error C2059: syntax error : '='
    c:\documents and settings\emanuele\desktop\array\circle.h(13) : error C2059: syntax error : ')'
    c:\documents and settings\emanuele\desktop\array\circle.h(13) : error C2238: unexpected token(s) preceding ';'
    c:\documents and settings\emanuele\desktop\array\circle.cpp(10) : error C2511: 'Circle::Circle' : overloaded member function 'void (double,int,int)' not found in 'Circle'
    c:\documents and settings\emanuele\desktop\array\circle.h(10) : see declaration of 'Circle'
    Error executing cl.exe.

    circle.obj - 6 error(s), 0 warning(s)
    ----------------------------------------------------------------------------------------
    mi potete aiutare??

  10. #10
    Utente di HTML.it L'avatar di KrOW
    Registrato dal
    Feb 2009
    Messaggi
    281
    L' errore sta nei parametri del costruttore di circle. . . Non puoi omettere i nomi se li dichiari di default (errore che hai fatto anche prima):
    codice:
     
    Circke(double d=0.0, int i=0, int j=0)

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.