Меню

Ошибка c2433 ostream friend не разрешается для объявлений данных

Hi, all.

I runned into a weird compiler error C2433. Seems that the compiler(VS2008) see a friend fucntion as a data member declaration.
(I haved post a thread about this( but not the same issue),
http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/4d212ad0-1474-4759-8f6f-a04189a11cf8
For more information about the code sample, you can access the above thread.)

Code snapshot (This code is just for describing my question):
namespace NSTest{
—declare a template function:
template<typename ET>
CV<ET> operator+( typename ET::priType src1, const CV<ET>& src2);

—declare the the specialization template  function as a class friend function:
template<typename ET>
class CV
{
public:
typedef CV<ET> SelfType;
friend SelfType operator+ <ET>( typename ET::priType src1, const SelfType& src2);
};

}//end of NSTest

————
The above code sample works in my sample example. But it does not work in my project.

In my project, I encount the following compiler error:
—————————
error C2143: syntax error : missing ‘;’ before ‘<‘
     see reference to class template instantiation ‘NSTest::CV<T>’ being compiled
error C2460: ‘NSTest::+’ : uses ‘NSTest::CV<T>’, which is being defined
error C2433: ‘NSTest::+’ : ‘friend’ not permitted on data declarations
error C2365: ‘NSTest::operator +’ : redefinition; previous definition was ‘function’
error C2238: unexpected token(s) preceding ‘;’
—————————
conclusion from the compiler error:
It seems that the compiler VS2008 see «friend SelfType operator+» as a data member declare.
The data member’s name is operator+, the type of the data member is SelfType.

So,  How Can I Bypass this compiler error.


Sorry for my English skill.I try my best to make the question clear.

Hi, all.

I runned into a weird compiler error C2433. Seems that the compiler(VS2008) see a friend fucntion as a data member declaration.
(I haved post a thread about this( but not the same issue),
http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/4d212ad0-1474-4759-8f6f-a04189a11cf8
For more information about the code sample, you can access the above thread.)

Code snapshot (This code is just for describing my question):
namespace NSTest{
—declare a template function:
template<typename ET>
CV<ET> operator+( typename ET::priType src1, const CV<ET>& src2);

—declare the the specialization template  function as a class friend function:
template<typename ET>
class CV
{
public:
typedef CV<ET> SelfType;
friend SelfType operator+ <ET>( typename ET::priType src1, const SelfType& src2);
};

}//end of NSTest

————
The above code sample works in my sample example. But it does not work in my project.

In my project, I encount the following compiler error:
—————————
error C2143: syntax error : missing ‘;’ before ‘<‘
     see reference to class template instantiation ‘NSTest::CV<T>’ being compiled
error C2460: ‘NSTest::+’ : uses ‘NSTest::CV<T>’, which is being defined
error C2433: ‘NSTest::+’ : ‘friend’ not permitted on data declarations
error C2365: ‘NSTest::operator +’ : redefinition; previous definition was ‘function’
error C2238: unexpected token(s) preceding ‘;’
—————————
conclusion from the compiler error:
It seems that the compiler VS2008 see «friend SelfType operator+» as a data member declare.
The data member’s name is operator+, the type of the data member is SelfType.

So,  How Can I Bypass this compiler error.


Sorry for my English skill.I try my best to make the question clear.

  • Remove From My Forums
  • Question

  • I have read all the referenced threads but can’t seem to find an answer that I can use.  I have the following code:

      friend ZDLLEXP ostream &
    operator << ( ostream &,
    const ZDate & );

    and it is giving me the following errors:

    1>c:tgm-us2zlibztime.h(33) : error C2433: ‘ostream’ : ‘friend’ not permitted on data declarations

    1>c:tgm-us2zlibztime.h(33) : error C4430: missing type specifier — int assumed. Note: C++ does not support default-int

    1>c:tgm-us2zlibztime.h(33) : error C2061: syntax error : identifier ‘ostream’

    1>c:tgm-us2zlibztime.h(33) : error C4430: missing type specifier — int assumed. Note: C++ does not support default-int

    1>c:tgm-us2zlibztime.h(33) : error C2805: binary ‘operator <<‘ has too few parameters

    Can you tesll me what is wrong?  I have seen it on numerous other times in this build.

Answers

    • Proposed as answer by

      Thursday, August 2, 2012 4:21 AM

    • Marked as answer by
      Elegentin Xie
      Tuesday, August 7, 2012 3:47 AM
  • I have read all the referenced threads but can’t seem to find an answer that I can use.  I have the following code:

      friend ZDLLEXP ostream &
    operator << ( ostream &,
    const ZDate & );

    and it is giving me the following errors:

    1>c:tgm-us2zlibztime.h(33) : error C2433: ‘ostream’ : ‘friend’ not permitted on data declarations

    1>c:tgm-us2zlibztime.h(33) : error C4430: missing type specifier — int assumed. Note: C++ does not support default-int

    1>c:tgm-us2zlibztime.h(33) : error C2061: syntax error : identifier ‘ostream’

    1>c:tgm-us2zlibztime.h(33) : error C4430: missing type specifier — int assumed. Note: C++ does not support default-int

    1>c:tgm-us2zlibztime.h(33) : error C2805: binary ‘operator <<‘ has too few parameters

    Can you tesll me what is wrong?  I have seen it on numerous other times in this build.

    May be because you are using ostream without qualifying it with namespace
    std::

    Also, note that there is an option to convert from «old» VC++ 6.0 projects:
    File->New->Project from existing code.

    • Proposed as answer by
      Elegentin Xie
      Thursday, August 2, 2012 4:21 AM
    • Marked as answer by
      Elegentin Xie
      Tuesday, August 7, 2012 3:47 AM

  • Remove From My Forums
  • Question

  • I have read all the referenced threads but can’t seem to find an answer that I can use.  I have the following code:

      friend ZDLLEXP ostream &
    operator << ( ostream &,
    const ZDate & );

    and it is giving me the following errors:

    1>c:tgm-us2zlibztime.h(33) : error C2433: ‘ostream’ : ‘friend’ not permitted on data declarations

    1>c:tgm-us2zlibztime.h(33) : error C4430: missing type specifier — int assumed. Note: C++ does not support default-int

    1>c:tgm-us2zlibztime.h(33) : error C2061: syntax error : identifier ‘ostream’

    1>c:tgm-us2zlibztime.h(33) : error C4430: missing type specifier — int assumed. Note: C++ does not support default-int

    1>c:tgm-us2zlibztime.h(33) : error C2805: binary ‘operator <<‘ has too few parameters

    Can you tesll me what is wrong?  I have seen it on numerous other times in this build.

Answers

    • Proposed as answer by

      Thursday, August 2, 2012 4:21 AM

    • Marked as answer by
      Elegentin Xie
      Tuesday, August 7, 2012 3:47 AM
  • I have read all the referenced threads but can’t seem to find an answer that I can use.  I have the following code:

      friend ZDLLEXP ostream &
    operator << ( ostream &,
    const ZDate & );

    and it is giving me the following errors:

    1>c:tgm-us2zlibztime.h(33) : error C2433: ‘ostream’ : ‘friend’ not permitted on data declarations

    1>c:tgm-us2zlibztime.h(33) : error C4430: missing type specifier — int assumed. Note: C++ does not support default-int

    1>c:tgm-us2zlibztime.h(33) : error C2061: syntax error : identifier ‘ostream’

    1>c:tgm-us2zlibztime.h(33) : error C4430: missing type specifier — int assumed. Note: C++ does not support default-int

    1>c:tgm-us2zlibztime.h(33) : error C2805: binary ‘operator <<‘ has too few parameters

    Can you tesll me what is wrong?  I have seen it on numerous other times in this build.

    May be because you are using ostream without qualifying it with namespace
    std::

    Also, note that there is an option to convert from «old» VC++ 6.0 projects:
    File->New->Project from existing code.

    • Proposed as answer by
      Elegentin Xie
      Thursday, August 2, 2012 4:21 AM
    • Marked as answer by
      Elegentin Xie
      Tuesday, August 7, 2012 3:47 AM

  • Forum
  • Beginners
  • Problem with this Complex Class

Problem with this Complex Class

Long story short, The code originally compiled without the ostream and istream. I don’t understand how those two work all that well, and operators aren’t my thing either.

It throws out 36 errors in between the ostream and istream.
The code is supposed to be able to read imaginary and real numbers, any tips, advice or anything to thrust me into the right direction would be greatly appreciated! Thanks CPP Forum ^^!

Header:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

  // Fig. 11.14: Complex.h
 // Complex class definition.
//a) Modify the class to enable input and output of complex numbers via overloaded >> and
//<< operators, respectively (you should remove the print function from the class).
//b) Overload the multiplication operator to enable multiplication of two complex numbers
//as in algebra.
//c) Overload the == and != operators to allow comparisons of complex numbers.
 #ifndef COMPLEX_H
 #define COMPLEX_H

 class Complex
 {

 public:

	 void setReal(double real);
	 void setImag(double imaginary);
	 double getReal() const;
	 double getImag() const;
	 Complex( double = 0.0, double = 0.0 ); // constructor
	 Complex operator+( const Complex & ) const; // addition
	 Complex operator-( const Complex & ) const; // subtraction
	 Complex operator ==(const Complex &); //Comparison
	 Complex operator !=(const Complex &); //Negative Comparison
	 Complex operator *(const Complex &) const; // Multiplication
	 friend ostream & operator << (ostream &output, const Complex &temp);
	 friend istream & operator >> (istream &input, const Complex &temp);


 // output
 private:
 double real; // real part
 double imaginary; // imaginary part
 }; // end class Complex

#endif

CPP Functions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Fig. 11.15: Complex.cpp
// Complex class member-function definitions.
 #include <iostream>
 #include "Complex.h" // Complex class definition
 using namespace std;

 // Constructor
 Complex::Complex( double realPart, double imaginaryPart )
 : real( realPart ),
 imaginary( imaginaryPart )
 {
 // empty body
 } // end Complex constructor

 // addition operator
 Complex Complex::operator+( const Complex &operand2 ) const
 {
 return Complex( real + operand2.real,
 imaginary + operand2.imaginary );
 } // end function operator+

 // subtraction operator
 Complex Complex::operator-( const Complex &operand2 ) const
 {
 return Complex( real - operand2.real,
 imaginary - operand2.imaginary );
 } // end function operator-

 //Multiplication Operator
 Complex Complex::operator*(const Complex & operand2) const
 {
	 return Complex(real * operand2.real, imaginary * operand2.imaginary );
 }

 Complex Complex::operator!=(const Complex & operand2)
 {
	 if(real != operand2.real){
		 real = operand2.real;
	return real;
	 }
 }
 Complex Complex::operator==(const Complex & operand2) 
 {
	  if(real == operand2.real){
		 real = operand2.real;
	return real;
	 }
 }

 	 void Complex::setReal(double realPart){
		real = realPart;
	 }
	 void Complex::setImag(double imaginaryPart){
		imaginary = imaginaryPart;
	 }
	 double Complex::getReal() const{
		 return	real;}

	 double Complex::getImag() const{
		 return	imaginary;}

 // display a Complex object in the form: (a, b)
	ostream & operator << (ostream & output, const Complex &temp){
		output << "" << temp.getReal() << endl;
		output << "" << temp.getImag() << endl;
		return output;
	 }
	istream & operator >> (istream &input, const Complex &temp){
		cout << "";
		input >> temp.getImag() << endl;
		cout <<"";
		input >> temp.getReal() << endl;
	}

Main File:

1
2
3
4
5
6
7
8
9
10
11
12
13
 #include "Complex.h"
 using namespace std;

 int main()
 {
 Complex x;
 Complex y( 4.3, 8.2 );
 Complex z( 3.3, 1.1 );


 system("PAUSE");
 return 0;
 }

The main used to be a print function, but our professor said to remove that part for the ostream and istream, and to be frank I barely have an idea to do it.

Again any help is greatly appreciated!
-Dreibs

> It throws out 36 errors in between the ostream and istream.
post them.

your header should be self-contained, if you want to use `ostream’ then you need to #include <iostream>
Also, its name is std::ostream

1
2
3
4
5
6
7
8
Complex /*bool*/ Complex::operator==(const Complex & operand2) /*const*/
 {
   if(real == operand2.real){
      real = operand2.real; //¿ah?
      return real;
   }
  //¿else what you return?
}

Last edited on

input >> temp.getImag() << endl;

«input» is an istream &, you can’t use «<<»

istream & operator >> (istream &input, const Complex &temp){
when reading input, the value of temp will be modified. const is not needed here as it will prevent such changes.

ne555 It gives 30 now after tinkering with it a bit. The errors are:

Error 3 error C4430: missing type specifier — int assumed. Note: C++ does not support default-int
Error 8 error C4430: missing type specifier — int assumed. Note: C++ does not support default-int
Error 13 error C4430: missing type specifier — int assumed. Note: C++ does not support default-int
Error 18 error C4430: missing type specifier — int assumed. Note: C++ does not support default-int
Error 25 error C4430: missing type specifier — int assumed. Note: C++ does not support default-int
Error 23 error C2872: ‘ostream’ : ambiguous symbol
Error 10 error C2805: binary ‘operator >>’ has too few parameters
Error 20 error C2805: binary ‘operator >>’ has too few parameters
Error 5 error C2805: binary ‘operator <<‘ has too few parameters
Error 15 error C2805: binary ‘operator <<‘ has too few parameters
Error 22 error C2511: ‘bool Complex::operator ==(const Complex &) const’ : overloaded member function not found in ‘Complex’
Error 21 error C2511: ‘bool Complex::operator !=(const Complex &) const’ : overloaded member function not found in ‘Complex’
Error 2 error C2433: ‘ostream’ : ‘friend’ not permitted on data declarations
Error 12 error C2433: ‘ostream’ : ‘friend’ not permitted on data declarations
Error 7 error C2433: ‘istream’ : ‘friend’ not permitted on data declarations
Error 17 error C2433: ‘istream’ : ‘friend’ not permitted on data declarations
Error 29 error C2275: ‘Complex’ : illegal use of this type as an expression
Error 1 error C2143: syntax error : missing ‘;’ before ‘&’
Error 6 error C2143: syntax error : missing ‘;’ before ‘&’
Error 11 error C2143: syntax error : missing ‘;’ before ‘&’
Error 16 error C2143: syntax error : missing ‘;’ before ‘&’
Error 24 error C2143: syntax error : missing ‘;’ before ‘&’
Error 26 error C2086: ‘int ostream’ : redefinition
Error 28 error C2065: ‘temp’ : undeclared identifier
Error 27 error C2065: ‘output’ : undeclared identifier
Error 4 error C2061: syntax error : identifier ‘ostream’
Error 14 error C2061: syntax error : identifier ‘ostream’
Error 9 error C2061: syntax error : identifier ‘istream’
Error 19 error C2061: syntax error : identifier ‘istream’
Error 30 error C1903: unable to recover from previous error(s); stopping compilation

For the most part I fixed what tipaye said about the input (Oops). and changed the const in the istream and ostream operators.

In a few of the errors it said that they needed more parameters, should I just add in new objects or is it something else?
Thanks in advance!

> after tinkering with it a bit.
If the errors do not correspond to the code, then they are both useless.

In `Complex.h’ add #include <iostream> and change the prototypes to

1
2
	 friend std::ostream & operator << (std::ostream &output, const Complex &temp);
	 friend std::istream & operator >> (std::istream &input, Complex &temp);

input >> temp.getImag();
`.getImage()’ returns a temporary, instead it should be

1
2
3
double imag;
input >> imag;
temp.setImag(imag);

Last edited on

Topic archived. No new replies allowed.

0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии

А вот еще интересные материалы:

  • Яшка сломя голову остановился исправьте ошибки
  • Ясность цели позволяет целеустремленно добиваться намеченного исправьте ошибки
  • Ясность цели позволяет целеустремленно добиваться намеченного где ошибка
  • Ошибка c2429 для функция языка структурированные привязки нужен флаг компилятора std c 17
  • Ошибка c1611 hyundai ix35