Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

pocetnički problem

[es] :: C/C++ programiranje :: C/C++ za početnike :: pocetnički problem

[ Pregleda: 4342 | Odgovora: 6 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

bane

Član broj: 449
Poruke: 226
*.teol.net



Profil

icon pocetnički problem13.02.2003. u 16:15 - pre 226 meseci
Ovo je primer iz “Osnove jezika C++”. Kompajler javlja greške koje jednostavno ne razumijem.

Matrix.cpp


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


Matrix operator+( const Matrix &m1, const Matrix &m2 ){
Matrix result( m1 );
result += m2;
return result;
}
Matrix operator*( const Matrix &m1, const Matrix &m2 ){
Matrix result;
for ( int ix = 0; ix < m1.rows(); ix++ )
for ( int jx = 0; jx < m1.cols(); jx++ ){
result( ix, jx ) = 0;
for ( int kx = 0; kx < m1.cols(); kx++ )
result( ix, jx ) += m1( ix, kx ) * m2( kx, jx );
}
return result;
}
void Matrix::operator+=( const Matrix &m ){
for ( int ix = 0; ix < 4; ++ix )
for ( int jx = 0; jx < 4; ++jx )
_matrix[ix][jx] += m._matrix[ix][jx];
}
inline ostream& Matrix::print( ostream &os ) const {
int cnt = 0;
for ( int ix = 0; ix < 4; ++ix )
for ( int jx = 0; jx < 4; ++jx, ++cnt ){
if ( cnt && !( cnt % 8 )) os << endl;
os << _matrix[ix][jx] << ' ';
}
os << endl;
return os;
}
inline ostream& operator<<( ostream& os, const Matrix &m )
{ return m.print( os ); }

int main()
{
Matrix m;
cout << m << endl;
elemType ar[16]={
1., 0., 0., 0., 0., 1., 0., 0.,
0., 0., 1., 0., 0., 0., 0., 1. };
Matrix identity( ar );
cout << identity << endl;
Matrix m2( identity );
m = identity;
cout << m2 << endl; cout << m << endl;
elemType ar2[16] = {
1.3, 0.4, 2.6, 8.2, 6.2, 1.7, 1.3, 8.3,
4.2, 7.4, 2.7, 1.9, 6.3, 8.1, 5.6, 6.6 };
Matrix m3( ar2 ); cout << m3 << endl;
Matrix m4 = m3 * identity; cout << m4 << endl;
Matrix m5 = m3 + m4; cout << m5 << endl;
m3 += m4; cout << m3 << endl;
}



Matrix.h


#include <iostream>
typedef float elemType;

class Matrix
{
friend Matrix operator+( const Matrix&, const Matrix& );
friend Matrix operator*( const Matrix&, const Matrix& );
public:
Matrix( const elemType* );
Matrix( elemType=0.,elemType=0.,elemType=0.,elemType=0.,
elemType=0.,elemType=0.,elemType=0.,elemType=0.,
elemType=0.,elemType=0.,elemType=0.,elemType=0.,
elemType=0.,elemType=0.,elemType=0.,elemType=0. );
int rows() const { return 4; }
int cols() const { return 4; }
ostream& print( ostream& ) const;
void operator+=( const Matrix& );
elemType operator()( int row, int column ) const
{ return _matrix[ row ][ column ]; }
elemType& operator()( int row, int column )
{ return _matrix[ row ][ column ]; }
private:
elemType _matrix[4][4];
};

inline Matrix::Matrix( const elemType *array ){
int array_index = 0;
for ( int ix = 0; ix < 4; ++ix )
for ( int jx = 0; jx < 4; ++jx )
_matrix[ix][jx] = array[array_index++];
}
inline Matrix::Matrix(
elemType a11, elemType a12, elemType a13, elemType a14,
elemType a21, elemType a22, elemType a23, elemType a24,
elemType a31, elemType a32, elemType a33, elemType a34,
elemType a41, elemType a42, elemType a43, elemType a44 )
{
_matrix[0][0] = a11; _matrix[0][1] = a12;
_matrix[0][2] = a13; _matrix[0][3] = a14;
_matrix[1][0] = a21; _matrix[1][1] = a22;
_matrix[1][2] = a23; _matrix[1][3] = a24;
_matrix[2][0] = a31; _matrix[2][1] = a32;
_matrix[2][2] = a33; _matrix[2][3] = a34;
_matrix[3][0] = a41; _matrix[3][1] = a42;
_matrix[3][2] = a43; _matrix[3][3] = a44;
}



1000 zasto 1000 zato
 
Odgovor na temu

Dragi Tata
Malo ispod Kanade

Član broj: 1958
Poruke: 3906
199.171.112.*



+6 Profil

icon Re: pocetnički problem13.02.2003. u 16:38 - pre 226 meseci
Kod mene ovo radi bez greške. Koji kompajler koristiš i koje greške dobijaš?
 
Odgovor na temu

bane

Član broj: 449
Poruke: 226
*.teol.net



Profil

icon Re: pocetnički problem13.02.2003. u 18:18 - pre 226 meseci
VC++ 6.0

Evo sta dobijam:

--------------------Configuration: Matrix - Win32 Debug--------------------
Compiling...
Matrix.cpp
c:\documents and settings\boris\my documents\c++\matrix\matrix.h(21) : error C2143: syntax error : missing ';' before '&'
c:\documents and settings\boris\my documents\c++\matrix\matrix.h(21) : error C2501: 'ostream' : missing storage-class or type specifiers
c:\documents and settings\boris\my documents\c++\matrix\matrix.h(21) : error C2061: syntax error : identifier 'ostream'
c:\documents and settings\boris\my documents\c++\matrix\matrix.h(21) : error C2501: 'print' : missing storage-class or type specifiers
c:\documents and settings\boris\my documents\c++\matrix\matrix.cpp(26) : error C2511: 'print' : overloaded member function 'class std::basic_ostream<char,struct std::char_traits<char> > &(class std::basic_ostream<char,struct std::char_traits<char> >
&) const' not found in 'Matrix'
c:\documents and settings\boris\my documents\c++\matrix\matrix.h(5) : see declaration of 'Matrix'
c:\documents and settings\boris\my documents\c++\matrix\matrix.cpp(37) : error C2660: 'print' : function does not take 1 parameters
c:\documents and settings\boris\my documents\c++\matrix\matrix.cpp(52) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
c:\documents and settings\boris\my documents\c++\matrix\matrix.cpp(52) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
c:\documents and settings\boris\my documents\c++\matrix\matrix.cpp(52) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
c:\documents and settings\boris\my documents\c++\matrix\matrix.cpp(52) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
c:\documents and settings\boris\my documents\c++\matrix\matrix.cpp(52) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
c:\documents and settings\boris\my documents\c++\matrix\matrix.cpp(52) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
c:\documents and settings\boris\my documents\c++\matrix\matrix.cpp(52) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
c:\documents and settings\boris\my documents\c++\matrix\matrix.cpp(52) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
c:\documents and settings\boris\my documents\c++\matrix\matrix.cpp(53) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
c:\documents and settings\boris\my documents\c++\matrix\matrix.cpp(53) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
c:\documents and settings\boris\my documents\c++\matrix\matrix.cpp(53) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
c:\documents and settings\boris\my documents\c++\matrix\matrix.cpp(53) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
c:\documents and settings\boris\my documents\c++\matrix\matrix.cpp(53) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
c:\documents and settings\boris\my documents\c++\matrix\matrix.cpp(53) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
c:\documents and settings\boris\my documents\c++\matrix\matrix.cpp(53) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
c:\documents and settings\boris\my documents\c++\matrix\matrix.cpp(53) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
c:\documents and settings\boris\my documents\c++\matrix\matrix.cpp(58) : warning C4508: 'main' : function should return a value; 'void' return type assumed
Error executing cl.exe.

Matrix.exe - 6 error(s), 17 warning(s)


[Ovu poruku je menjao bane dana 13.02.2003. u 20:23 GMT]
1000 zasto 1000 zato
 
Odgovor na temu

Dragi Tata
Malo ispod Kanade

Član broj: 1958
Poruke: 3906
199.171.112.*



+6 Profil

icon Re: pocetnički problem13.02.2003. u 18:21 - pre 226 meseci
I ja isto. Koje greške dobijaš?
 
Odgovor na temu

bane

Član broj: 449
Poruke: 226
*.teol.net



Profil

icon Re: pocetnički problem14.02.2003. u 11:20 - pre 226 meseci
Zamenio sam:

<iostream> sa <iostream.h> i
izbrisao liniju : using namespace std;



Ovo stvarno radi samo da li postoji neko objašnjenje zašto je to tako. Mislim probao sam ovo na dva različita kompa koji su imali instaliran XP stim što sam VC++ snimao sa istog diska. Pre toga sam koristio ovaj kompajler i sve je radilo normalo (using namespace std; itd). Zašto baš u ovom slučaju nije.
1000 zasto 1000 zato
 
Odgovor na temu

Dragi Tata
Malo ispod Kanade

Član broj: 1958
Poruke: 3906
199.171.112.*



+6 Profil

icon Re: pocetnički problem14.02.2003. u 16:46 - pre 226 meseci
To nemoj da radiš - bolje ubaci using namespace std na početak h fajla.

A što se tiče pitanja "zašto", odovor će ti se kazati kad pročitaš malo više o "namespaces" u nekoj knjizi u C++u. Ovo ipak nije mesto da se objašnjavaju takve stvari.
 
Odgovor na temu

X Files
Vladimir Stefanovic
Pozarevac

SuperModerator
Član broj: 15100
Poruke: 4889
*.3dnet.co.yu.

Jabber: xfiles@elitesecurity.org


+633 Profil

icon Re: pocetnički problem15.05.2006. u 09:09 - pre 187 meseci
Izbegavaj foldere koji imaju + u nazivu (npr c++), pa cak i # (npr C#). Bolje daj naziv cpp.
To ce ti resiti i neke buduce probleme...
 
Odgovor na temu

[es] :: C/C++ programiranje :: C/C++ za početnike :: pocetnički problem

[ Pregleda: 4342 | Odgovora: 6 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.