Error c2784 c

Still fighting with templates. In this example, despite the fact that is copied straight from a book I'm getting the following error message: Error 2 error C2784: 'IsClassT::One IsClassT&l...

Still fighting with templates. In this example, despite the fact that is copied straight from a book I’m getting the following error message: Error 2 error C2784: 'IsClassT<T>::One IsClassT<T>::test(int C::* )' : could not deduce template argument for 'int C::* ' from 'int'.

This is an example from a book Templates — The Complete Guide.
(I work with Visual Studio 2010 RC).

  template<typename T> 
    class IsClassT { 
      private: 
        typedef char One; 
        typedef struct { char a[2]; } Two; 
        template<typename C> static One test(int C::*); 
        template<typename C> static Two test(…); 
      public: 
        enum { Yes = sizeof(IsClassT<T>::test<T>(0)) == 1 }; 
        enum { No = !Yes }; 
    }; 

class MyClass { 
}; 

struct MyStruct { 
}; 

union MyUnion { 
}; 

void myfunc() 
{ 
} 

enum E {e1} e; 

// check by passing type as template argument 
template <typename T> 
void check() 
{ 
    if (IsClassT<T>::Yes) { 
        std::cout << " IsClassT " << std::endl; 
    } 
    else { 
        std::cout << " !IsClassT " << std::endl; 
    } 
} 

// check by passing type as function call argument 
template <typename T> 
void checkT (T) 
{ 
    check<T>(); 
} 

int main() 
{ 
    /*std::cout << "int: "; 
    check<int>(); */

    std::cout << "MyClass: "; 
    check<MyClass>(); 
}

And although I know roughly what’s going on in this example I cannot fix this error.
Thanks for help.

Bill's user avatar

Bill

14.1k4 gold badges42 silver badges54 bronze badges

asked Mar 16, 2010 at 13:50

There is nothing we can do's user avatar

10

My compiler (MSVC2008TS) likes it if you don’t fully qualify the test expression:

enum { Yes = sizeof(test<T>(0)) == 1 }; 

But is this even legal code?

answered Mar 16, 2010 at 14:23

John Dibling's user avatar

John DiblingJohn Dibling

98.8k29 gold badges183 silver badges321 bronze badges

0

Shouldn’t this line

    enum { Yes = sizeof(IsClassT<T>::test<T>(0)) == 1 }; 

be

    enum { Yes = sizeof(IsClassT<T>::template test<T>(0)) == 1 }; 

instead?`

(And because I’m anal, I’d write sizeof(IsClassT<T>::template test<T>(0)) == sizeof(One).)

answered Mar 16, 2010 at 15:16

sbi's user avatar

5

Not exactly an answer to this question, but rather a different approach to your problem. I find it easier to do SFINAE tests in specializations rather than in functions defined in that template:

// used to pass a type tested with SFINAE
template<typename T> struct tovoid { typedef void type; };

Using it to pass a type that may be invalid:

template<typename T, typename U = void> 
struct is_class {
  static bool const value = false;
};

// if "int T::*" is a valid type, this specialization is used
template<typename T>
struct is_class<T, typename tovoid<int T::*>::type> {
  static bool const value = true;
};

This way it’s considerably shorter and the noise with sizeof and things aren’t done.

answered Mar 17, 2010 at 7:38

Johannes Schaub - litb's user avatar

  • Remove From My Forums
  • Question

  • I looked the similar posted (and often answered) issues. It seems like this problem may related to the new C+0x standard. However, I need to figure out a way to build it. So any workaround, or the right way would be appreciated :)

    The error message:

    error C2784: ‘Vec<D, T> round(const Vec<D,T> &)’: cannot deduce const Vec <D,T> &’ template argument from ‘float’

    #define VEC_DECLARE_ONEARG(name) 
     template <int D, class T> 
     static inline Vec<D,T> name(const Vec<D,T> &v) 
     { ... }
    
    VEC_DECLARE_ONEARG(round)
    
    template <class T>
    static inline T clamp(const T &x, const T &a, const T &b)
    { ... }
    
    
    clamp(int(round(n->col[0] * 255.0f)), 0, 255)
    
    • Edited by

      Tuesday, November 9, 2010 3:32 AM
      Typo corrected

Answers

  • There is no surer way whether VC6 has the float round(float) than testing with installed VC6. But don’t you have other versions of Visual Studio installed? Would those higher version of Visual Studio installation supersede the definition of math.h?
    Does VCs keep their own math.h?

    I think there are three possible conclusions then

    1. VCs have never supported round, at least from the VC6.

    2. If higher version of VC installed then math.h is replaced accordingly so we can be absolutely sure about round() if we install VC6 before higher version of a VC is installed.

    3. The author used mingw or something like that on Windows in order to build the source with more C99 standard friendly compiler.

    Regardless the version of VC, I think so far we can say that the Vec<D,T> round(const Vec<D,T>) was never intended when ’round(n->col[0] … )’ called but ‘float round(float)’ defined in math.h.  

    In short, a simple program as below will not compile with VCs but gcc. (Tested with VC10 and gcc 4.1)

    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    int main()
    {
     cout << round(1.5f) << endl; 
    }
    

    I have found another MSDN thread about VC8.0 (so I think so forth) does not fully support the C99 standard since they focus on C++ standard.

    http://social.msdn.microsoft.com/forums/en-US/vcgeneral/thread/913a2001-d2a6-450a-a15e-ff97ab6da0f1/

    There is some consistent statement in a Wikipedia page about C99 standard.

    «there has been less support from vendors such as Microsot and Borland that have mainly focused on C++» (en.wikipedia.org/wiki/C99, fetched on 13 Nov 2010)

    It would be better and more authoritative if I could have found a list of supported functions of C99 standard published by Visual Studio team. But my quick web search didn’t get to that.

    If those statements are correct, then we can say that VCs are not fully up to C99 standards. As we know at least now is that ‘float round(float)’ is one of those not supported.


    Lee Seongjoo
    Ph.D. Candidate
    Computer Science
    Yonsei Univerisity
    Seoul, Korea

    • Marked as answer by
      Lee Seongjoo
      Saturday, November 13, 2010 2:04 AM
    • Edited by
      Lee Seongjoo
      Saturday, November 13, 2010 2:06 AM

  • lseongjoo wrote:

    There is no surer way whether VC6 has the float round(float) than  testing with installed VC6. But don’t you have other versions
    of Visual Studio installed?

    I do.

    Would those higher version of Visual Studio installation supersede the  definition of math.h? Does VCs
    keep their own math.h?

    Each version comes with its own set of headers and libraries. You could  configure one to use the other’s, I suppose, but it would be unwise.

    In any case, no VC version comes with round() function in math.h, to  my knowledge.

    1. VCs have never supported round, at least from the VC6.

    I believe this to be the case.

    2. If higher version of VC installed then math.h is replaced  accordingly

    This absolutely doesn’t happen.

    3. The author used mingw or something like that on Windows in order to  build the source with more C99 standard friendly compiler.

    Quite possible.


    Igor Tandetnik

    • Marked as answer by
      Lee Seongjoo
      Saturday, November 13, 2010 2:05 AM

AliceO

0 / 0 / 1

Регистрация: 08.11.2014

Сообщений: 6

1

18.11.2014, 10:19. Показов 1650. Ответов 2

Метки нет (Все метки)


имеется шаблонный список

C++
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "stdafx.h"
#include <iostream>
using namespace std;
 
template  <typename TYPE>
class list
{  
private:
 
    class node //Класс node содержит данные списка
    {
        friend class list<TYPE>;
    private:
        node *next;
        node *prev;
        TYPE val; //Данные списка
        node(): next(NULL){}
        ~node(){}
    };
    node *_begin;
    node *_end;
    node* make_node(const TYPE &data)
    {   
        node *Ptr = new node;
        Ptr->val = data;
        Ptr->prev = NULL;
        Ptr->next = NULL;
        return Ptr;
    }
 
public:
    class iterator
    {
        friend class list<TYPE>;
    private:
        node *nodeptr;
 
    public:
        iterator &operator++()
        {
            nodeptr = nodeptr->next;
            return *this;
        }
        iterator &operator--()
        {
            nodeptr = nodeptr->prev;
            return *this;
        }
 
        TYPE &iterator::operator*()
        {
            return nodeptr->val;
        }
 
        TYPE &iterator::operator->()
        {
            return nodeptr->val;
        }
    };
 
    list(): _begin(NULL), _end(NULL){}
    ~list();
 
    iterator begin()
    {
        static iterator b;
        b.nodeptr = _begin;
        return b;
    }
        
    iterator end()
    {
        static iterator e;
        e.nodeptr = _end; 
        return e;
    }
 
    void push_front(const TYPE);
    TYPE ReadFromPosition(int);
    bool pop(iterator);
    int empty() const; 
    void push_back(const TYPE );
    void clear();
};
 
template <typename TYPE>
list<TYPE>::~list()
{
    clear();
}
 
//Проверено
template <typename TYPE>
void list<TYPE>::push_front(const TYPE data)
{
    node *NewPtr = make_node(data);
    if(empty())
    {
        _begin = _end = NewPtr;
    }
    else 
    {
        NewPtr->next = _begin;
        _begin = NewPtr;
    }
}
 
template <typename TYPE>
void list<TYPE>::clear()
{
    node *current;
    current = _begin;
    node *to_delete = NULL;
    if(!empty())
    {
        while(current != NULL)
        {
            to_delete = current;
            current = current->next;
            delete to_delete;
        }
    }
}
 
template <typename TYPE>
void list<TYPE>::push_back(const TYPE data)
{
    node *NewPtr = make_node(data);
    if(empty())
        _begin = _end = NewPtr;
    else 
    {
        _end->next = NewPtr;
        _end = NewPtr;
    }
}
 
template <typename TYPE>
bool list<TYPE>::pop(iterator pos)
{
    int temp = 0,temp1 = 0;
    node *NewPtr = _begin;
    node *TempPtr = _begin;
    node *TempPtr1 = _begin;
    node *TempPtr2 = _begin;
    if(empty())
        return false;
    else
    {
        while(NewPtr->next)
        {
            temp ++;
            NewPtr = NewPtr->next;
        }
        if(temp < pos || pos < 0)
        {
            cout<<"Удаление невозможно"<<endl;
            return false;
        }
        while(temp1! = pos)  
        {
            TempPtr = TempPtr->next;
            temp1 ++;
        }
        if(pos == 0)
        {
            _begin = TempPtr->next;
            delete TempPtr;
            TempPtr = _begin;
            return true;
        }
        if(TempPtr == _end) 
        {
            while(TempPtr1->next! = _end) 
                TempPtr1 = TempPtr1->next;
            delete _end;
            _end = TempPtr1;
            _end->next = NULL;
            return true;
        }
        while(TempPtr2->next! = TempPtr) 
            TempPtr2 = TempPtr2->next;
        TempPtr2->next = TempPtr->next;
        delete TempPtr;
        return true;
 
    }
}
 
template <typename TYPE>
TYPE list<TYPE>::ReadFromPosition(int pos)
{
    int temp = 0,temp1 = 0;
    node *NewPtr = _begin;
    node *TempPtr = _begin;
    if(empty()) return NULL;
    while(NewPtr) {temp ++;NewPtr = NewPtr->next;}
    if(pos>temp || pos<0) return NULL;
    if(pos == 0) return _begin->val;
     pos --; 
    while(pos)
    {
        pos --; 
        TempPtr = TempPtr->next;         
    }
    return TempPtr->val;
}
//Проверено
template <typename TYPE>
int list<TYPE>::empty() const
{
    if(_begin == NULL)
        return 1;
    else return 0;
}
 
void main()
{
    list<int> l;
    int new_int, c;
    cout<<"Enter list"<<endl;
    for(int c = 0; c < 5; c++)
    {
        cout<<(c + 1)<<endl;
        cin>>new_int;
        l.push_back(new_int);
    }
    list<int>::iterator it = l.begin();
    for(;it != l.end(); it ++) //в этом месте ошибка
        cout<<*it<<endl;
}

error C2784: ‘bool std::operator !=(const std::istreambuf_iterator<_Elem,_Traits> &,const std::istreambuf_iterator<_Elem,_Traits> &)’ : could not deduce template argument for ‘const std::istreambuf_iterator<_Elem,_Traits> &’ from ‘list<TYPE>::iterator’
1> with
1> [
1> TYPE=int
1> ]

__________________
Помощь в написании контрольных, курсовых и дипломных работ, диссертаций здесь



0



Вездепух

Эксперт CЭксперт С++

10435 / 5704 / 1553

Регистрация: 18.10.2014

Сообщений: 14,098

18.11.2014, 10:26

2

Цитата
Сообщение от AliceO
Посмотреть сообщение

could not deduce template argument

Ну так а оператор ‘!=’ вы для своего итератора определили? Не определили.
А пользоваться ‘!=’ пытаетесь? Пытаетесь.

Компилятор в отчаянии пытается приспособить какой-то другой оператор ‘!=’, но у него не получается. Поэтому ошибка.

Вперед определять операторы ‘==’ и ‘!=’ для вашего итератора.



0



AliceO

0 / 0 / 1

Регистрация: 08.11.2014

Сообщений: 6

18.11.2014, 18:05

 [ТС]

3

Спасибо за ответ.
Вот волшебство, дописываю в методы итератора

C++
1
2
3
4
5
6
7
    bool iterator::operator!=(iterator compare)
        {
            if(this->nodeptr != compare.nodeptr)
                return true;
            else
                return false;
        }

и

fatal error C1001: An internal error has occurred in the compiler

Добавлено через 16 минут
ессс! работает
спасибо ещё раз



0



Home  »  Programming   »   C++ Error C2784 – Working with GUID keys in map data structures

Error C2784 is caused by problems with the compiler for using a key in map data structures. Map data structures are very useful in C++, their C# equivalent being the Dictionary structure. They allow you to store data to a key, which means when you come to need that data you can use the key, rather than iterating through a vector or similar.

Error C2784 – Could not deduce template argument

C++ Error C2784

C++ Error C2784

What is it? Error C2784 highlights that the key you are using in your map structure cannot deduce a template argument from a lack of a ‘<‘ operator.
Common causes C2784 can be caused by any key data type which does not contain an overloaded ‘<‘ operator. Keys are sorted by the map structure, however, if there is no ‘<‘ operator to perform comparisons with, the keys cannot be sorted and, as such, C++ will not compile until it can.
How to fix it? This error can be fixed by providing an overload for the ‘<‘ operator yourself. The example below shows how to deal with the GUID data structure, which is a common key in maps due to the unique nature of GUIDs (i.e. no two GUIDs will be the same allowing for unique key access). The example given below can be adapted for any data structure which does not contain the necessary ‘<‘ operator.

Example fix

The following code is for using GUIDs as keys, but can be adapted as necessary for any data type which needs to fix this error. Commonly, you would put any overloaded operators in the class in which they relate (i.e. if you created your own co-ordinate class and needed to overload the ‘<‘ operator you would do so in that class) however, this may not always be possible particularly if you are overloading it for a class you have not made. I myself use a common tools file in which I contain some common methods which may be needed across several classes, which is where I place this fix for GUID key mapping.

inline bool operator< (const GUID &firstGUID, const GUID &secondGUID) {
    return (memcmp(&firstGUID, &secondGUID, sizeof(GUID)) < 0 ? true : false);
}

About Author

FraserG

Computer Scientist currently undertaking an Engineering Doctorate degree discussing computing, programming, research and racing.

  • Forum
  • General C++ Programming
  • Error C2784 — Please Help!

Error C2784 — Please Help!

Hi there, I’m new to this forum and pretty new to c++ as well and I can’t seem to get this error figured out. Here is the full error message:

C2784: ‘std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &&,std::basic_string<_Elem,_Traits,_Alloc> &)’ : could not deduce template argument for ‘std::basic_istream<_Elem,_Traits> &&’ from ‘std::ostream’

Here is my C++ code:

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()

{

string name;
double roomcharge,extracharge,total;
double roomnum,numofdays;
const double roomrate=125;

cout << fixed;

cout << «Please fill in the following information to output the Total room charges, and an invouce for the guest: » << endl << endl;
cout << «Customer Name: «;
getline(cin, name);
cout << «Room Number: «;
cin >> roomnum;
cout << «Number of Days Stayed: «;
cin >> numofdays;
cout << «Extra Charges: «;
cin >> extracharge;

roomcharge=roomrate*numofdays;
total=roomcharge+extracharge;

cout << «Star Hotel Customer Invoice: «;
cout << «Customer Name: «;

system(«pause»);

}

My Project is still incomplete at the moment, but I don’t want to continue until I get this error fixed.

Any help would be appreciated, thanks in advance.

Since you don’t have something like cout >> name ; anywhere in your code, you should not be getting this error.

Hm, well that’s really weird because it’s still giving me issues. I think I’ll start a new project in Visual Studio and re-enter the code. Thanks for your input.

I’ll post my results here after trying a new project.

I expect the code you think you’re compiling is not the code you are compiling.

Moschops you may be right. I made a new project and just pasted the same code in, and it built successfully. No idea how or why, but it’s fine now. Thanks for all of the help :)

I didn’t think I had any errors in my code >.> <.<

Topic archived. No new replies allowed.

Got the above error, when compiling something that looked like this:

std::map<std::string, myType> myMap;
myMap[“Test”] = myType();

Looked around on the net, didn’t find the answer, thought about it for a bit and then realised I was missing #include <string> which fixed the problem. The error was a bit cryptic. Look for yourself at this dump from the compiler output:

 1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)’ : could not deduce template argument for ‘const std::_Tree<_Traits> &’ from ‘const std::string’

1>        C:Program FilesMicrosoft Visual Studio 8VCincludextree(1372) : see declaration of ‘std::operator <‘

1>        C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(142) : while compiling class template member function ‘bool std::less<_Ty>::operator ()(const _Ty &,const _Ty &) const’

1>        with

1>        [

1>            _Ty=std::string

1>        ]

1>        C:Program FilesMicrosoft Visual Studio 8VCincludemap(72) : see reference to class template instantiation ‘std::less<_Ty>’ being compiled

1>        with

1>        [

1>            _Ty=std::string

1>        ]

1>        C:Program FilesMicrosoft Visual Studio 8VCincludextree(26) : see reference to class template instantiation ‘std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,_Mfl>’ being compiled

1>        with

1>        [

1>            _Kty=std::string,

1>            _Ty=std::string,

1>            _Pr=std::less<std::string>,

1>            _Alloc=std::allocator<std::pair<const std::string,std::string>>,

1>            _Mfl=false

1>        ]

1>        C:Program FilesMicrosoft Visual Studio 8VCincludextree(68) : see reference to class template instantiation ‘std::_Tree_nod<_Traits>’ being compiled

1>        with

1>        [

1>            _Traits=std::_Tmap_traits<std::string,std::string,std::less<std::string>,std::allocator<std::pair<const std::string,std::string>>,false>

1>        ]

1>        C:Program FilesMicrosoft Visual Studio 8VCincludextree(94) : see reference to class template instantiation ‘std::_Tree_ptr<_Traits>’ being compiled

1>        with

1>        [

1>            _Traits=std::_Tmap_traits<std::string,std::string,std::less<std::string>,std::allocator<std::pair<const std::string,std::string>>,false>

1>        ]

1>        C:Program FilesMicrosoft Visual Studio 8VCincludextree(112) : see reference to class template instantiation ‘std::_Tree_val<_Traits>’ being compiled

1>        with

1>        [

1>            _Traits=std::_Tmap_traits<std::string,std::string,std::less<std::string>,std::allocator<std::pair<const std::string,std::string>>,false>

1>        ]

1>        C:Program FilesMicrosoft Visual Studio 8VCincludemap(82) : see reference to class template instantiation ‘std::_Tree<_Traits>’ being compiled

1>        with

1>        [

1>            _Traits=std::_Tmap_traits<std::string,std::string,std::less<std::string>,std::allocator<std::pair<const std::string,std::string>>,false>

1>        ]

1>        c:projectsflewsesourcex-planeflewseVisualSettings.h(16) : see reference to class template instantiation ‘std::map<_Kty,_Ty>’ being compiled

1>        with

1>        [

1>            _Kty=std::string,

1>            _Ty=std::string

1>        ]

1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)’ : could not deduce template argument for ‘const std::_Tree<_Traits> &’ from ‘const std::string’

1>        C:Program FilesMicrosoft Visual Studio 8VCincludextree(1372) : see declaration of ‘std::operator <‘

1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)’ : could not deduce template argument for ‘const std::_Tree<_Traits> &’ from ‘const std::string’

1>        C:Program FilesMicrosoft Visual Studio 8VCincludextree(1372) : see declaration of ‘std::operator <‘

1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)’ : could not deduce template argument for ‘const std::_Tree<_Traits> &’ from ‘const std::string’

1>        C:Program FilesMicrosoft Visual Studio 8VCincludextree(1372) : see declaration of ‘std::operator <‘

1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)’ : could not deduce template argument for ‘const std::vector<_Ty,_Alloc> &’ from ‘const std::string’

1>        C:Program FilesMicrosoft Visual Studio 8VCincludevector(1276) : see declaration of ‘std::operator <‘

1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)’ : could not deduce template argument for ‘const std::vector<_Ty,_Alloc> &’ from ‘const std::string’

1>        C:Program FilesMicrosoft Visual Studio 8VCincludevector(1276) : see declaration of ‘std::operator <‘

1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)’ : could not deduce template argument for ‘const std::vector<_Ty,_Alloc> &’ from ‘const std::string’

1>        C:Program FilesMicrosoft Visual Studio 8VCincludevector(1276) : see declaration of ‘std::operator <‘

1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)’ : could not deduce template argument for ‘const std::vector<_Ty,_Alloc> &’ from ‘const std::string’

1>        C:Program FilesMicrosoft Visual Studio 8VCincludevector(1276) : see declaration of ‘std::operator <‘

1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)’ : could not deduce template argument for ‘const std::reverse_iterator<_RanIt> &’ from ‘const std::string’

1>        C:Program FilesMicrosoft Visual Studio 8VCincludexutility(1880) : see declaration of ‘std::operator <‘

1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)’ : could not deduce template argument for ‘const std::reverse_iterator<_RanIt> &’ from ‘const std::string’

1>        C:Program FilesMicrosoft Visual Studio 8VCincludexutility(1880) : see declaration of ‘std::operator <‘

1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)’ : could not deduce template argument for ‘const std::reverse_iterator<_RanIt> &’ from ‘const std::string’

1>        C:Program FilesMicrosoft Visual Studio 8VCincludexutility(1880) : see declaration of ‘std::operator <‘

1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)’ : could not deduce template argument for ‘const std::reverse_iterator<_RanIt> &’ from ‘const std::string’

1>        C:Program FilesMicrosoft Visual Studio 8VCincludexutility(1880) : see declaration of ‘std::operator <‘

1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)’ : could not deduce template argument for ‘const std::pair<_Ty1,_Ty2> &’ from ‘const std::string’

1>        C:Program FilesMicrosoft Visual Studio 8VCincludeutility(76) : see declaration of ‘std::operator <‘

1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)’ : could not deduce template argument for ‘const std::pair<_Ty1,_Ty2> &’ from ‘const std::string’

1>        C:Program FilesMicrosoft Visual Studio 8VCincludeutility(76) : see declaration of ‘std::operator <‘

1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)’ : could not deduce template argument for ‘const std::pair<_Ty1,_Ty2> &’ from ‘const std::string’

1>        C:Program FilesMicrosoft Visual Studio 8VCincludeutility(76) : see declaration of ‘std::operator <‘

1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)’ : could not deduce template argument for ‘const std::pair<_Ty1,_Ty2> &’ from ‘const std::string’

1>        C:Program FilesMicrosoft Visual Studio 8VCincludeutility(76) : see declaration of ‘std::operator <‘

1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2676: binary ‘<‘ : ‘const std::string’ does not define this operator or a conversion to a type acceptable to the predefined operator

I am very new to object oriented programming and C++. I have been working on a matrix class and squarematrix class and have been running into some problems that I can’t seem to figure out. The error code I have been getting is:

C2784:
'matrix<T,m,k> operator *(matrix<T,m,n> &,matrix<T,n,k> &)': could not
deduce template argument for 'matrix<T,m,n> &' from
'std::vector<std::vector<double,std::allocator<_Ty>>,std::allocator<std::vector<_Ty,std::allocator<_Ty>>>>'

I am really unsure why, because I have had other parts of my code work. The error is reported in the line with «product.elements = product.elements * elements;»

//Source.cpp
#include"Squarematrix.h"
#include<iostream>
#include<vector>
using namespace std;
int main() {
    vector<double> a = { 1, 2,4,5,6};
    squarematrix<double,2> N;
    N.assign(a);
    cout << N << N.pow(2)<< endl;
    return(0);
}

//Matrix.h
#ifndef _Matrix_
#define _Matrix_
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
template<class T, int m, int n>
class matrix {
public:
    vector<vector<T>> elements;
    int nrow;
    int ncol;
    matrix();
    matrix(matrix<T, m, n>&);
 };
template<class T, int m, int n>
matrix<T, m, n>::matrix() {
    vector<T>temp(n, 0);
    elements.assign(m, temp);
    nrow = m;  //m=0
    ncol = n;  //n=0
}
template<class T, int m, int n>
matrix<T, m, n>::matrix(matrix<T, m, n>& a) {
    elements = a.elements;
    nrow = m;
    ncol = n;
}
template<class T, int m, int n, int k>
matrix<T, m, k> operator*(const matrix<T, m, n>& a, const matrix<T, n, k>& b) {
matrix<T, m, k> product;
for (int i = 0; i < m; i++) {
    for (int j = 0; j < k; j++) {
        for (int h = 0; h < n; h++)
            product.elements[i][j] += a.elements[i][h] * b.elements[h][j];
    }
}
return product;
}

template<class T, int m, int n>
ostream& operator<< (ostream& o, const matrix<T, m, n>& input) {
    for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n; ++j)
    o << input.elements[i][j] << " ";
    o << endl;
    }
    return o;
}
#endif _Matrix_

//Squarematrix.h
#ifndef _Squarematrix_
#define _Squarematrix_

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

template<class T, int n>
class squarematrix : public matrix<T, n, n> {
public:
    squarematrix();
    squarematrix(squarematrix<T, n>&);

    squarematrix<T, n> pow(int); //calculate A^k
};
template<class T, int n>
squarematrix<T, n>::squarematrix(){
    vector<T>temp(n, 0);
    elements.assign(n, temp);
    nrow = n;  //n=0
    ncol = n;  //n=0
}
template<class T, int n>
squarematrix<T, n>::squarematrix(squarematrix<T, n>& a){
    elements = a.elements;
    nrow = n;
    ncol = n;
}
template<class T, int n>
squarematrix<T, n> squarematrix<T, n>::pow(int k){
    squarematrix<T, n> product;
    product.elements = elements;
    for (int power = 2; power <= k; power++) {
    product.elements = product.elements * elements;
    }
    return product;
}
#endif _Squarematrix_


You don’t need nrow and ncol — they’re template parameters and known at compile time.

But that’s not the problem — you’re multiplying std::vector where you should be multiplying squarematrix:

template<class T, int n>
squarematrix<T, n> squarematrix<T, n>::pow(int k){
    squarematrix<T, n> product = unit_matrix<T, n>();
    for (int power = 1; power <= k; power++) {
        product = product * *this;
    }
    return product;
}

where I’ve used a fictitious function that creates a unit matrix.
Writing that function left as an exercise.

Понравилась статья? Поделить с друзьями:
  • Error c2712 cannot use try in functions that require object unwinding
  • Error c2679 бинарный не найден оператор принимающий правый операнд типа
  • Error c2679 binary no operator found which takes a right hand operand of type
  • Error c2678 binary no operator found which takes a left hand operand of type
  • Error c2664 in c