Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

Problem z Modułowością

Ostatnio zmodyfikowano 2014-11-27 17:14
Autor Wiadomość
Sasquacz
Temat założony przez niniejszego użytkownika
Problem z Modułowością
» 2014-11-27 17:11:56
Hej,
Na wstępie mowie, że dopiero zaczynam zabawę z programowaniem. Pierwszy raz robie program modułowy i odrazu mam problem którego nie umiem rozwiązać mianowicie:

Main
C/C++
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "type.h"

using namespace std;

int he;
int we;
;




int main()
{
    cout << "Podaj wysokosc: ";
    cin >> we;
   
    cout << "Podaj szerokosc: ";
    cin >> he;
   
   
    cout << "Zostanie stworzony obiekt obraz1.";
   
   
    cout << "Width = " << obraz1.getWidth();
    cout << "Height = " << obraz1.getHeight();
    system( "PAUSE" );
   
    cout << endl << "Teraz dokona sie zapis pikseli" << endl;
    obraz1.putPixel( 0, 0, 50 );
    obraz1.putPixel( 0, 1, 120 );
    obraz1.putPixel( 1, 0, 99 );
    obraz1.putPixel( 1, 1, 200 );
   
    cout << endl << "Piksel na pozycji [3,3] ma wartosc: " << obraz1.getPixel( 3, 3 ) << endl;
    cout << endl << "Piksel na pozycji [0,0] ma wartosc: " << obraz1.getPixel( 0, 0 ) << endl;
    system( "PAUSE" );
   
    cout << endl << "Wyswietlenie obrazu" << endl;
    obraz1.print();
    system( "PAUSE" );
   
    cout << "Tworzenie kopii" << endl;
    Image obraz2( obraz1 );
   
    cout << "Width = " << obraz1.getWidth();
    cout << "Height = " << obraz1.getHeight();
    system( "PAUSE" );
   
    /* cout<<endl<<"Wyswietlenie kopii "<<endl;
        obraz2.print();
        system("PAUSE");*/
   
    cout << endl << "Nadawanie jasnosci piksela ." << endl;
    obraz2.fillInImage( 0 );
    system( "PAUSE" );
   
    cout << endl << "Wyswietlenie kopii" << endl;
    obraz2.print();
   
    cout << "\n\n\n";
    system( "PAUSE" );
    return 0;
   
    getchar();
   
   
}


Image.h

C/C++
#include <iostream>
#include <cstdlib>
#include "type.h"

using namespace std;



class Image
{
private:
    byte ** tab; //tablica przechowująca wartości pikseli obrazu
    int width;
    int height;
    //utworzenie tablicy
    void dealloc(); //zniszczenie tablicy
public:
    Image(); //konstruktor
    Image( int width, int height ); //konstruktor
    Image( const Image & image ); //konstruktor kopiujący
    Image( const Image & image, int x, int y, int w, int h ); //konstruktor kopiujący
    ~Image(); //destruktor
    Image & operator =( const Image & image ); //operator przypisania
    int getWidth() const; //zwraca width
    int getHeight() const; //zwraca height
    void assignFrom( const Image & image, int x, int y, int w, int h ); //kopiuje zawartość innego obrazka
    byte getPixel( int x, int y ); //zwraca wartość (kolor) piksela na pozycji [x, y]
    void putPixel( int x, int y, byte value ); //zapis piksela
    void fillInImage( byte value ); //nadaje ten sam kolor wszystkim pikselom
    void print(); //wyświetlenie obrazka
}

//#endif






void Image::alloc( int width, int height ) //utworzenie tablicy
{
    this->width = width;
    this->height = height;
    tab = new byte *[ width ];
    for( int i = 0; i < width; ++i )
         tab[ i ] = new byte[ height ];
   
}




void Image::dealloc() //zniszczenie tablicy
{
    for( int i = 0; i < width; ++i )
         delete[] tab[ i ];
   
    delete[] tab;
}



Image::Image() //konstruktor
{
    alloc( 0, 0 );
   
}



Image::Image( int width, int height ) //konstruktor
{
    alloc( width, height );
   
}




Image::Image( const Image & image ) //konstruktor kopiujący
{
    alloc( 0, 0 );
    * this = image;
   
}


Image::Image( const Image & image, int x, int y, int w, int h ) //konstruktor kopiujący
{
    alloc( 0, 0 );
    assignFrom( image, x, y, w, h );
   
}


Image::~Image() //destruktor
{
    dealloc();
   
}



void Image::operator =( const Image & image ) //operator przypisania
{
    assignFrom( image, 0, 0, image.getWidth(), image.getHeight() );
}


int Image::getWidth() const //zwraca width
{
    return width;
}


int Image::getHeight() const //zwraca height
{
    return height;
}


void Image::assignFrom( const Image & image, int x, int y, int w, int h ) //kopiuje zawartość innego obrazka
{
    dealloc();
    alloc( w, h );
    for( int i = 0; i < w; ++i )
    {
        for( int j = 0; j < h; ++j )
        {
            tab[ i ][ j ] = int( image.tab[ x + i ][ y + j ] );
        }
    }
}



byte Image::getPixel( int x, int y ) //zwraca wartość (kolor) piksela na pozycji [x, y]
{
    if( x > width || y > height )
    {
        cout << "Blad! Podane indeksy wykraczaja poza obszar obrazu.";
        cout << "Wpisz indeksy maksymalnie do [" << getWidth() << "][" << getHeight() << "].";
    }
    else
         return tab[ x ][ y ];
   
}




void Image::putPixel( int x, int y, byte value ) //zapis piksela
{
    if( x > width || y > height )
    {
        cout << "Blad! Podane indeksy wykraczaja poza obszar obrazu.";
        cout << "Wpisz indeksy maksymalnie do [" << getWidth() << "][" << getHeight() << "].";
    }
    else
   
         tab[ x ][ y ] = value;
   
}



void Image::fillInImage( byte value ) //nadaje ten sam kolor wszystkim pikselom
{
    for( int i = 0; i < width; i++ )
    {
        for( int j = 0; j < height; j++ )
        {
            tab[ i ][ j ] = value;
        }
    }
}



void Image::print() //wyświetlenie obrazka
{
    for( int i = 0; i < width; i++ )
    {
        for( int j = 0; j < height; j++ )
        {
            if( tab[ i ][ j ] <= 100 )
                 cout << "X";
            else
                 cout << " ";
           
        }
    }
}


Type.h

C/C++
#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;

typedef unsigned char byte;


Problem następuje gdy z Maina przeniosłem do Image.h to co sie w nim znajduje:D. Wyskakuje mi bład

[Error] 'obraz1' was not declared in this scope


Jak go rozwiązać? jakieś odniesienie ze obraz znajduje sie teraz w image.h?

Pozdrawiam
P-121553
NopeDotAvi
» 2014-11-27 17:14:53
i skąd zabrałeś kod? W ogóle nie opisałeś problemu, wstawiłeś cały kod. Ogranicz ilość wstawianego kodu do minimum

@@ok napisałeś.
Nie stworzyłeś obiektu w mainie. By to zrobić to na górze tam gdzie masz "include" różne dodaj nową linikę:
#include "NazwaPlikuWKtorymJestClass Image.h

i w
int main()
 dodaj
Image obraz1;
P-121555
« 1 »
  Strona 1 z 1