c++_problem

Programovacie jazyky, rady, poradňa...
johny3212
Star
Star
Používateľov profilový obrázok
Príspevky: 532
Registrovaný: 17 feb 2008, 19:56
Bydlisko: Terchova
Kontaktovať používateľa:

c++_problem

Príspevok od používateľa johny3212 »

Mam problem s funkciou, vypisuje mi:
expected initializer before ‘MyArray’
problem je vo funkciach typu inline, vid nizzsie

Kód: Vybrať všetko

namespace MyTools
{

template<class Typ>
class MyArray
  {
private:
  Typ *Data;
  int Lower, Size; // Spodní hranice indexu, počet prvků v poli


public:
  //============ type
  typedef Typ value_type;
  typedef Typ &reference;
  typedef const Typ &const_reference;
  typedef Typ *iterator;
  typedef const Typ *const_iterator;

  inline iterator begin();
  inline iterator end();
};

//tu je problem v definicii (expected initializer before ‘MyArray’)
template<class Typ> inline MyArray<Typ>::iterator MyArray<Typ>::begin()
{
  return Data;
}
}
prikladam cely zdrojak, skuste si to skompilovat, komu sa chce :), diik.

Kód: Vybrať všetko


/*
 * MyArray.h
 *
 *  Created on: 3.8.2009
 *      Author: johnyB
 */

#ifndef MYARRAY_H_
#define MYARRAY_H_

#include <iostream>
#include <algorithm>

using namespace std;
namespace MyTools
{

template<class Typ>
class MyArray
  {
private:
  Typ *Data;
  int Lower, Size; // Spodní hranice indexu, počet prvků v poli


public:
  //============ type
  typedef Typ value_type;
  typedef Typ &reference;
  typedef const Typ &const_reference;
  typedef Typ *iterator;
  typedef const Typ *const_iterator;
  typedef size_t size_type;
  typedef int int1;
  // size_t je s největší pravděpodobností unsigned int

  typedef ptrdiff_t difference_type;
  // ptrdiff_t je s největší pravděpodobností int


  // ========= Konstruktory, destruktor a operátor =
  MyArray();
  MyArray(const MyArray<Typ> &copy);
  MyArray(difference_type low, difference_type up);
  MyArray<Typ> &operator=(const MyArray<Typ> &copy);
  ~MyArray();

  // Iterátory
  inline int vypis();
  inline iterator begin();
  inline iterator end();
  inline const_iterator begin() const;
  inline const_iterator end() const;

  // Náhodný přístup k prvkům - operátor [], metoda at
  inline const_reference operator[](difference_type index) const;
  inline reference operator[](difference_type index);
  inline const_reference at(difference_type index) const;
  inline reference at(difference_type index);

  // Relační operátory
  bool operator==(const MyArray<Typ> &second) const;
  inline bool operator!=(const MyArray<Typ> &second) const;
  inline bool operator<(const MyArray<Typ> &second) const;
  inline bool operator>(const MyArray<Typ> &second) const;

  // swap
  void swap(MyArray<Typ> &second);

  // Metody pro informování o kapacitě, velikosti a indexech
  inline size_type size() const;
  inline size_type max_size() const;
  inline size_type capacity() const;
  inline difference_type getLower() const;
  inline difference_type getUpper() const;
  inline bool empty() const;

  };

//*****************************************************************************

// Implicitni konstruktor
template<class Typ>
MyArray<Typ>::MyArray() :
  Data(NULL), Lower(0), Size(0)
  {
  }

// Kopirovaci konstruktor
// Parametry:
//       const Array &copy - reference na pole, ktere bude kopirovano
template<class Typ>
MyArray<Typ>::MyArray(const MyArray &copy) :
  Data(NULL), Lower(copy.getLower()), Size(copy.size())
  {
    register iterator d;
    Data = new Typ[this->size()];
    d = begin();
    for (register const_iterator p = copy.begin(); p < copy.end(); p++, d++)
      {
        *d = *p;
      }

  }

// Konstruktor vytvarejici pole s indexy od low, do up (vcetne)
// Parametry:
//       difference_type low - spodni hranice indexu
//       difference_type up - horni hranice indexu
// Poznamky:
//       difference_type je s nejvetsi pravdepodobnosti int
template<class Typ>
MyArray<Typ>::MyArray(difference_type low, difference_type up) :
  Lower(low), Size(up - low + 1)
  {

    Data = new Typ[size()];

  }

// Operator =
// Parametry:
//       const Array &copy - Reference na pole, ktere bude kopirovano.
// Navratova hodnota:
//       Array<Typ> - Reference na aktualni pole. Nutne pro "skladani"
//                    operatoru = "za sebe". Napr. a = b =c;
template<class Typ>
MyArray<Typ> &MyArray<Typ>::operator=(const MyArray<Typ> &copy)
  {

    delete[] Data;
    Size = copy.size();
    Lower = copy.getLower();
    Data = new Typ[size()];
    register iterator d = this->begin();
    for (register const_iterator t = copy.begin(); t < copy.end(); t++, d++)
      {
        *d = *t;
      }

    return *this;

  }

// Destruktor
template<class Typ>
MyArray<Typ>::~MyArray()
  {
    delete[] Data;
    Data = NULL;
  }


template<class Typ>
int MyArray<Typ>::vypis(){

  return 1;
}

// Metoda begin
// Navratova hodnota:
//       iterator - Iterator odkazujici na zacatek pole
template<class Typ> inline MyArray<Typ>::iterator MyArray<Typ>::begin()
{
  return Data;
}

// Metoda end
// Navratova hodnota:
//       iterator - Iterator odkazujici za konec pole

template<class Typ>
inline MyArray<Typ>::iterator MyArray<Typ>::end()
{
  return &Data[size()];
}




}


#endif /* MYARRAY_H_ */


tuti
Light Professional
Light Professional
Používateľov profilový obrázok
Príspevky: 740
Registrovaný: 01 okt 2006, 19:23
Bydlisko: Prievidza

Príspevok od používateľa tuti »

skus odmazat to inline u definicie iteratora (u deklaracie moze kludne ostat ale neviem ci to bude potom prekladac vkladat inline..aj tak si vacsina prekladacov so slovom inline robi co chce)...problem je v samotnom klucovom slove inline pouziteho tam kde si ho dal... ;)

ak by to nepomohlo tak skus napisat definiciu inline fcie priamo do triedy
Napísať odpoveď