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

Ошибки при перегрузке операторов класса

15.05.2018, 01:34. Показов 536. Ответов 1


Студворк — интернет-сервис помощи студентам

Задали в универе для класа триугольника с полями 3х сторон реализовать перегрузку операторов <<, !=, ==, <, >, <=, >=.
Делал все по примерам, как нужно, но компилятор с этим в корне не согласен. Ни в какую не могу понять в чем проблема, подскажите пожалуйста где и в чем не прав

Кликните здесь для просмотра всего текста

1>—— Сборка начата: проект: lab4, Конфигурация: Debug Win32 ——
1> triangle.cpp
1>c:usersqwakabydocumentsvisual studio 2015projectslab4lab4triangle.h(23): error C2143: синтаксическая ошибка: отсутствие «;» перед «&»
1>c:usersqwakabydocumentsvisual studio 2015projectslab4lab4triangle.h(23): error C2433: ostream: «friend» не разрешается для объявлений данных
1>c:usersqwakabydocumentsvisual studio 2015projectslab4lab4triangle.h(23): error C4430: отсутствует спецификатор типа — предполагается int. Примечание. C++ не поддерживает int по умолчанию
1>c:usersqwakabydocumentsvisual studio 2015projectslab4lab4triangle.h(23): error C2238: непредвиденные лексемы перед «;»
1>c:usersqwakabydocumentsvisual studio 2015projectslab4lab4triangle.cpp(55): error C2872: ostream: неоднозначный символ
1> c:usersqwakabydocumentsvisual studio 2015projectslab4lab4triangle.h(23): note: может быть «int ostream»
1> c:program files (x86)microsoft visual studio 14.0vcincludeiosfwd(679): note: или «std::ostream»
1>c:usersqwakabydocumentsvisual studio 2015projectslab4lab4triangle.cpp(55): error C2143: синтаксическая ошибка: отсутствие «;» перед «&»
1>c:usersqwakabydocumentsvisual studio 2015projectslab4lab4triangle.cpp(55): error C4430: отсутствует спецификатор типа — предполагается int. Примечание. C++ не поддерживает int по умолчанию
1>c:usersqwakabydocumentsvisual studio 2015projectslab4lab4triangle.cpp(55): error C2086: int ostream: переопределение
1> c:usersqwakabydocumentsvisual studio 2015projectslab4lab4triangle.h(23): note: см. объявление «ostream»
1>c:usersqwakabydocumentsvisual studio 2015projectslab4lab4triangle.cpp(55): error C2065: str: необъявленный идентификатор
1>c:usersqwakabydocumentsvisual studio 2015projectslab4lab4triangle.cpp(55): error C2065: tr: необъявленный идентификатор
1>c:usersqwakabydocumentsvisual studio 2015projectslab4lab4triangle.cpp(55): error C2275: triangle: недопустимое использование этого типа в качестве выражения
1> c:usersqwakabydocumentsvisual studio 2015projectslab4lab4triangle.h(3): note: см. объявление «triangle»
1>c:usersqwakabydocumentsvisual studio 2015projectslab4lab4triangle.cpp(55): error C2473: operator <<: выглядит как определение функции, но без списка параметров.
1>c:usersqwakabydocumentsvisual studio 2015projectslab4lab4triangle.cpp(56): error C2448: <<: вероятно, инициализатор, использующий стиль функции, является определением функции
1> lab4.cpp
1>c:usersqwakabydocumentsvisual studio 2015projectslab4lab4triangle.h(23): error C2143: синтаксическая ошибка: отсутствие «;» перед «&»
1>c:usersqwakabydocumentsvisual studio 2015projectslab4lab4triangle.h(23): error C2433: ostream: «friend» не разрешается для объявлений данных
1>c:usersqwakabydocumentsvisual studio 2015projectslab4lab4triangle.h(23): error C4430: отсутствует спецификатор типа — предполагается int. Примечание. C++ не поддерживает int по умолчанию
1>c:usersqwakabydocumentsvisual studio 2015projectslab4lab4triangle.h(23): error C2238: непредвиденные лексемы перед «;»
1>c:usersqwakabydocumentsvisual studio 2015projectslab4lab4lab4.cpp(32): error C2679: бинарный «<<«: не найден оператор, принимающий правый операнд типа «triangle» (или приемлемое преобразование отсутствует)
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(495): note: может быть «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(std::basic_streambuf<char,std::char_traits<char>> *)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(475): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(const void *)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(455): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(long double)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(435): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(double)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(415): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(float)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(395): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(unsigned __int64)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(375): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(__int64)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(355): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(unsigned long)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(335): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(long)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(315): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(unsigned int)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(290): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(int)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(270): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(unsigned short)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(236): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(short)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(216): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(bool)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(209): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(std::ios_base &(__cdecl *)(std::ios_base &))»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(202): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(std::basic_ios<char,std::char_traits<char>> &(__cdecl *)(std::basic_ios<char,std::char_traits<char>> &))»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(196): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(std::basic_ostream<char,std::char_traits<char>> &(__cdecl *)(std::basic_ostream<char,std::char_traits<char>> &))»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(1019): note: или «std::basic_ostream<char,std::char_traits<char>> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char> > &,const std::error_code &)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(981): note: или «std::basic_ostream<char,std::char_traits<char>> &std::operator <<<char,std::char_traits<char>,triangle>(std::basic_ostream<char,std::char_trait s<char>> &&,const _Ty &)»
1> with
1> [
1> _Ty=triangle
1> ]
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(971): note: или «std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,unsigned char)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(964): note: или «std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,const unsigned char *)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(957): note: или «std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,signed char)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(950): note: или «std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,const signed char *)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(824): note: или «std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,char)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(777): note: или «std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,const char *)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(739): note: или «std::basic_ostream<char,std::char_traits<char>> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char> > &,char)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(692): note: или «std::basic_ostream<char,std::char_traits<char>> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char> > &,const char *)»
1> c:usersqwakabydocumentsvisual studio 2015projectslab4lab4lab4.cpp(32): note: при попытке сопоставить список аргументов «(std::ostream, triangle)»
1>c:usersqwakabydocumentsvisual studio 2015projectslab4lab4lab4.cpp(33): error C2679: бинарный «<<«: не найден оператор, принимающий правый операнд типа «triangle» (или приемлемое преобразование отсутствует)
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(495): note: может быть «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(std::basic_streambuf<char,std::char_traits<char>> *)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(475): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(const void *)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(455): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(long double)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(435): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(double)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(415): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(float)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(395): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(unsigned __int64)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(375): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(__int64)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(355): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(unsigned long)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(335): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(long)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(315): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(unsigned int)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(290): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(int)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(270): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(unsigned short)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(236): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(short)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(216): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(bool)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(209): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(std::ios_base &(__cdecl *)(std::ios_base &))»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(202): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(std::basic_ios<char,std::char_traits<char>> &(__cdecl *)(std::basic_ios<char,std::char_traits<char>> &))»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(196): note: или «std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(std::basic_ostream<char,std::char_traits<char>> &(__cdecl *)(std::basic_ostream<char,std::char_traits<char>> &))»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(1019): note: или «std::basic_ostream<char,std::char_traits<char>> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char> > &,const std::error_code &)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(981): note: или «std::basic_ostream<char,std::char_traits<char>> &std::operator <<<char,std::char_traits<char>,triangle>(std::basic_ostream<char,std::char_trait s<char>> &&,const _Ty &)»
1> with
1> [
1> _Ty=triangle
1> ]
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(971): note: или «std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,unsigned char)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(964): note: или «std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,const unsigned char *)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(957): note: или «std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,signed char)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(950): note: или «std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,const signed char *)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(824): note: или «std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,char)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(777): note: или «std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,const char *)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(739): note: или «std::basic_ostream<char,std::char_traits<char>> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char> > &,char)»
1> c:program files (x86)microsoft visual studio 14.0vcincludeostream(692): note: или «std::basic_ostream<char,std::char_traits<char>> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char> > &,const char *)»
1> c:usersqwakabydocumentsvisual studio 2015projectslab4lab4lab4.cpp(33): note: при попытке сопоставить список аргументов «(std::ostream, triangle)»

—————main.cpp————-

C++ (Qt)
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
#include <iostream>
#include "triangle.h"
using namespace std;
 
void set_obj(triangle &tmp_obj);
void out(triangle &tr1, triangle &tr2);
void set_obj(triangle &tmp_obj) 
{
    int a, b, c;
    cout << "Enter 1st side : ";
    cin >> a;
    tmp_obj.set_a(a);
    cout << endl;
    cout << "Enter 2nd side : ";
    cin >> b;
    tmp_obj.set_b(b);
    cout << endl;
    cout << "Enter 3th side : ";
    cin >> c;
    tmp_obj.set_c(c);
    cout << endl;
}
 
void main()
{
    triangle tr1;
    set_obj(tr1);
    triangle tr2;
    set_obj(tr2);
    int per = tr1.perimeter();
    double area = tr1.area();
    cout << tr1;
    cout << tr2;
    /*cout << "Perimetr 1 : " << per << "Area 1 : " << area << endl;
    per = tr2.perimeter;
    area = tr2.area;
    cout << "Perimetr 2 : " << per << "Area 2 : " << area << endl;
    if (tr1 == tr2)
    {
        cout << "Area 1 = area 2" << endl;
    }
    if (tr1 != tr2)
    {
        cout << "Area 1 <> Area 2" << endl;
    }
    if (tr1 <= tr2)
    {
        cout << "Area 1 <= Area 2" << endl;
    }
    if (tr1 >= tr2)
    {
        cout << "Area 1 >= Area 2" << endl;
    }
    if (tr1 < tr2)
    {
        cout << "Area 1 < Area 2" << endl;
    }
    if (tr1 > tr2)
    {
        cout << "Area 1 > Area 2" << endl;
    } */
    tr1.~triangle();
    tr2.~triangle();
}

——————-triangle.h————

C++ (Qt)
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
#pragma once
class triangle
{
private:
    int a_len;
    int b_len;
    int c_len;
public:
    int get_a();
    int get_b();
    int get_c();
 
    void set_a(int tmp);
    void set_b(int tmp);
    void set_c(int tmp);
 
    void a_out();
    void b_out();
    void c_out();
 
    int perimeter();
    double area();
 
    friend ostream& operator << (ostream &s,  triangle &tr);
    const friend bool operator < (triangle &t1, triangle &t2);
    const friend bool operator > (triangle &t1, triangle &t2);
    const friend bool operator == (triangle &t1, triangle &t2);
    const friend bool operator != (triangle &t1, triangle &t2);
    const friend bool operator <= (triangle &t1, triangle &t2);
    const friend bool operator >= (triangle &t1, triangle &t2);
 
    triangle();
    triangle(int A_len, int B_len, int C_len);
    ~triangle();
};

————-triangle.cpp—————

C++ (Qt)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "triangle.h"
#include <iostream>
#include <cmath>
 
using namespace std;
 
int triangle::get_a() 
{
    return a_len;
}
int triangle::get_b()
{
    return b_len;
}
int triangle::get_c()
{
    return c_len;
}
 
void triangle::set_a(int tmp) 
{
    a_len = tmp;
}
void triangle::set_b(int tmp)
{
    b_len = tmp;
}
void triangle::set_c(int tmp)
{
    c_len = tmp;
}
 
void triangle::a_out() 
{
    cout << "1st side : " << a_len << endl;
}
void triangle::b_out()
{
    cout << "2nd side : " << b_len << endl;
}
void triangle::c_out()
{
    cout << "3th side : " << c_len << endl;
}
 
double triangle::area()
{
    return sqrt(pow(a_len, 2) + pow(b_len, 2) + pow(c_len, 2));
}
int triangle::perimeter() 
{
    return a_len + b_len + c_len;
}
 
const bool operator < (triangle &t1, triangle &t2) 
{
    double S1 = t1.area();
    double S2 = t2.area();
    return S1 < S1;
}
const bool operator > (triangle &t1, triangle &t2)
{
    double S1 = t1.area();
    double S2 = t2.area();
    return S1 > S1;
}
const bool operator == (triangle &t1, triangle &t2)
{
    double S1 = t1.area();
    double S2 = t2.area();
    if (S1 == S2) 
    {
        return true;
    }
    else 
    {
        return false; 
    }
}
const bool operator != (triangle &t1, triangle &t2)
{
    double S1 = t1.area();
    double S2 = t2.area();
    if (S1 != S2)
    {
        return true;
    }
    else
    {
        return false;
    }
}
const bool operator <= (triangle &t1, triangle &t2)
{
    double S1 = t1.area();
    double S2 = t2.area();
    return S1 <= S1;
}
const bool operator >= (triangle &t1, triangle &t2)
{
    double S1 = t1.area();
    double S2 = t2.area();
    return S1 >= S1;
}
 
 
triangle::triangle():a_len(1),b_len(1),c_len(1)
{
}
triangle::triangle(int A_len, int B_len, int C_len) :a_len(A_len), b_len(B_len), c_len(C_len) 
{
}
triangle::~triangle()
{
}



0



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.

I am working on this project just trying to keep up my c++ knowledge. Anyways, I am getting many, many errors when i try to implement an operator overload. Not sure why.

#include "students.h"
#include <iostream>
#include "Quack.h"

using namespace std;

void main()
{


quack* classmates = new quack;

classmates->pushFront(students("corey", "9081923456", 4.0));

cout << "noriginal data set -- " << *students;

and this is where i am getting the errors with the operator. Oddly enough if i comment out the overloaded operator and leave it in students.cpp it compiles find.

#ifndef STUDENTS_H
#define STUDENTS_H
#include <iostream>

class students
{
      // causing errors
friend ostream& operator << (ostream& out,const students& student);

public:
students();
students(char * name, char* oitId, float gpa);
students(const students& student); // copy constructor;
 ~students();
const students& operator=(const students& student);

void getName(char* name) const;
void getoitId(char* oitId) const;
float getGpa(void) const;

void setName(char* name);
void setoitId(char* oitId);
void setGpa(float gpa);


private:
char*    name;
char*    oitId;
float    gpa;

};

#endif

}

and, causes errors alo. But not by itself..

#include "students.h"
#include <iostream>
#include <iomanip>

using namespace std;
#pragma warning(disable:4996)       

private:
char* name;
char* oitId;
float gpa;

students::students(): name(NULL), oitId(NULL), gpa(0)
{
}


students::students(char *name, char *oitId, float gpa): name(NULL), oitId(NULL), gpa(0)
{
setName(name);
setoitId(oitId);

}

students::~students()
{

if(name)
delete[] name;
if(oitId)
delete[] oitId;

}




const students& students::operator=(const students& student)
{

//if it is a self copy, don't do anything
if(this == &student)
    return *this;
//make current object *this a copy of the passed in student
else
{
    setName(student.name);
    setoitId(student.oitId);
    //setGpa(student.gpa);
    return *this;
}

}


void students::setName(char *name)
{

//release the exisisting memory if there is any
if(this->name)
delete [] this->name;

//set new name
this->name = new char[strlen(name)+1];
strcpy(this->name, name);

}

void students::setoitId(char *oitId)
{

if(this->oitId)
delete [] this->oitId;

//set new Id
this->oitId = new char[strlen(oitId)+1];
strcpy(this->oitId, oitId);

}

ostream& operator<< (ostream& out, const students& student)
{

//out << setw(20) << student.name
    //<< setw(15) << student.pccId
    //<< setw(8) << fixed << setprecision(2) << student.gpa;
return out;
}

here is the errors i get

syntax error : missing ‘;’ before ‘&’
: error C2433: ‘ostream’ : ‘friend’ not permitted on data declarations
error C4430: missing type specifier — int assumed. Note: C++ does not support default-int
error C2061: syntax error : identifier ‘ostream’
error C4430: missing type specifier — int assumed. Note: C++ does not support default-int
error C2805: binary ‘operator <<‘ has too few parameters
1>Generating Code…
1>Compiling…
1>students.cpp

my eyes are burning and i cant figure out why its unhappy with the overloaded operator..

Permalink

Cannot retrieve contributors at this time

description title ms.date f1_keywords helpviewer_keywords ms.assetid

Learn more about: Compiler Error C2433

Compiler Error C2433

11/04/2016

C2433

C2433

7079fedd-6059-4125-82ef-ebe275f1f9d1

Compiler Error C2433

‘identifier’ : ‘modifier’ not permitted on data declarations

The friend, virtual, and inline modifiers cannot be used for data declarations.

Example

The following sample generates C2433.

// C2433.cpp
class C{};

int main() {
   inline C c;   // C2433
}
  • Forum
  • General C++ Programming
  • overloading << and >> operators

overloading << and >> operators

I am trying to overload these two operations for a class, and I am getting really strange errors.
I’ve looked at a couple guides and I can’t find what I am doing wrong with my declaration.
#include <iostream>
class Complex{

friend ostream& operator<< (ostream& out, const Complex& right);
friend istream& operator>> (istream& in, const Complex& right);
};

The errors I am getting:
error C2143: syntax error : missing ‘;’ before ‘&’
error C2433: ‘ostream’ : ‘friend’ not permitted on data declarations
error C4430: missing type specifier — int assumed. Note: C++ does not support default-int
error C2061: syntax error : identifier ‘ostream’
error C4430: missing type specifier — int assumed. Note: C++ does not support default-int
error C2805: binary ‘operator <<‘ has too few parameters

Any help would be much appreciated.

It looks right to me…you sure you didn’t forget a ; before those definitions or something.

I’m glad, I’m not completely crazy.
I’m not missing any semicolons, though, and if I comment out that bit of code the program compile no problem. :/

Did you forget the std:: in front of ostream/istream?

Oh, you also don’t want a const on the operator >> Complex.

Ahhh, that fixed it! Thank you. Boy, I feel stupid now.
Weirdly, I think I had that earlier, but then I deleted the version of it when I was having problems. Oh well, it’s fixed now :)

Topic archived. No new replies allowed.

  • Ошибка c241301 kia rio
  • Ошибка c240201 kia какой электромотор
  • Ошибка c2402 kia rio
  • Ошибка c2402 hyundai santa fe
  • Ошибка c2402 hyundai elantra