|
Sasha119121 0 / 0 / 0 Регистрация: 21.09.2017 Сообщений: 13 |
||||
|
1 |
||||
|
28.10.2017, 23:24. Показов 7652. Ответов 1 Метки нет (Все метки)
error: ‘__comp’ cannot be used as a function|
__________________
0 |
|
Заклинатель змей 611 / 508 / 213 Регистрация: 30.04.2016 Сообщений: 2,412 |
|
|
29.10.2017, 10:42 |
2 |
|
РешениеSasha119121, ошибки надо полностью читать In file included from /usr/include/c++/6/bits/char_traits.h:39:0, Ошибка в строке 22; 29, min должен принимать два аргумента
1 |
I am getting «‘_comp’ cannot be used as a function» error in the stl_algobase.h header file. Here is my code and the part of the header file that is supposed to have the error.
code:
#include<iostream>
#include<algorithm>
using namespace std;
void subsqr(int a[10][10]){
//int s[][10];
for(int i =1;i<5;i++){
for(int j = 1;j<5;j++){
if(a[i][j] == 1){
a[i][j] = min(a[i][j-1], a[i-1][j],a[i-1][j-1]) + 1;
}
}
}
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
cout<<a[i][j]<<"t";
}
cout<<endl;
}
}
int main(){
int a[10][10] = {{0,1,1,0,1}, {1,1,0,1,0}, {1,1,1,0}, {1,1,1,1,0}, {1,1,1,1,1}, {0,0,0,0,0}};
subsqr(a);
return 0;
}
stl_algobase.h:
template<typename _Tp, typename _Compare>
inline const _Tp&
min(const _Tp& __a, const _Tp& __b, _Compare __comp)
{
//return __comp(__b, __a) ? __b : __a;
if (__comp(__b, __a))
return __b;
return __a;
}
The compiler says that the error is in the line
if (__comp(__b, __a))
void new_doubles(double a, double b, double c)
{
double d=max(abs(a), abs(b), abs(c));
cout<<d;
}
производит эту ошибку времени компиляции:
In file included from /usr/local/gcc4.9.0/include/c++/4.9.0/bits/char_traits.h:39:0,
from /usr/local/gcc4.9.0/include/c++/4.9.0/string:40,
from /usr/local/gcc4.9.0/include/c++/4.9.0/stdexcept:39,
from /usr/local/gcc4.9.0/include/c++/4.9.0/array:38,
from std_lib_facilities_4.h:27,
from hw4pr4.cpp:1: /usr/local/gcc4.9.0/include/c++/4.9.0/bits/stl_algobase.h: In instantiation of ‘const _Tp& std::max(const _Tp&, const _Tp&, _Compare) [with _Tp = double; _Compare = double]’:
hw4pr4.cpp:35:36: required from here
/usr/local/gcc4.9.0/include/c++/4.9.0/bits/stl_algobase.h:264:26: error: ‘__comp’ cannot be used as a function
if (__comp(__a, __b))
Я не понимаю, почему возникает эта ошибка, может кто-нибудь помочь объяснить, что я делаю неправильно?
0
Решение
Единственная перегрузка std::max с тремя аргументами используется для получения большего из первых двух, интерпретируя третий как функцию сравнения для использования вместо < сравнить значения. Это не дает наибольшее из трех значений.
Чтобы получить наибольшее из трех значений, используйте перегрузку, которая принимает список инициализатора:
double d=max({abs(a), abs(b), abs(c)});
^ ^
Или, если вы застряли в мире до C ++ 11, дважды вызовите перегрузку для двух значений:
double d=max(abs(a), max(abs(b), abs(c)));
^^^^ ^
5
Другие решения
Макс не принимает три аргумента так, как вы думаете.
double d=max(max(abs(a), abs(b)), abs(c));
1
@GWW верен, но если вы используете компилятор C ++ 11, есть новый вариационный вариант — вам просто нужно предоставить список инициализаторов с несколькими аргументами вместо нескольких:
void new_doubles(double a, double b, double c)
{
double d = max( { abs(a), abs(b), abs(c) } );
cout << d;
}
0
|
|||||

|
yet-another-average-joe
Junior Member Posts: 27 Joined: Jul 27th, 2019 |
[BUG] Weird compilation issue with ArduinoSTL library (C++ templates) |
Print Post |
|
Hello, (not sure this is the right place for compilation errors) This bug report is about code that compiles and works fine in Arduino IDE, but fails in Visual Micro. Arduino IDE 1.8.9 Library : ArduinoSTL : https://github.com/mike-matera/ArduinoSTL MC : STM32 (STM32F103C8T6 Black Pill) First of all, the ArduinoSTL library is ‘seen» by the Arduino IDE library manager, NOT by the VisualMicro manager… In order to be able to include it in a Visual Micro project, just open the file library.properties, and set the field «architectures» to ‘*’ Then, download the two archives, TestSTL_1.zip and TestSTL_2.zip. They contain two tiny sketches that will show the problem. The two sketches compile just fine in Arduino IDE, but TestSTL_1 does not compile in Visual Micro. I suspect some offending gcc command line option in Visual Micro. I wasn’t able to find where. |
||
|
« Last Edit: Jul 27th, 2019 at 2:45pm by yet-another-average-joe » |
||
|
Please Register or Login to the Forum to see File Attachments |
||
|
|
|
Tim@Visual Micro Administrator Posts: 11684 Joined: Apr 10th, 2010 |
Re: [BUG] Weird compilation issue with ArduinoSTL library (C++ templates) |
Print Post |
|
Thanks for the three reports. It would help if you followed the guide in the yellow box and posted the full output with error. Thanks Some info 1) In the Arduino IDE, libraries with invalid architectures will not show on the Libraries menu but will attempt to compile. In this case the first libray it can find that contains a valid header is used. By coincidence, just published a few hours ago, the next release of Visual Micro attempts to adjust/fix the architectures of libraries where they can possibly be deduced through other logic and finally for libraries that have entirely different architecture spec, an attempt will still be made to compile. You can read more in the work thread in the releases section of this forum. https://www.visualmicro.com/forums/YaBB.pl?board=VS_ARDUINO_EXT_RELEASES 2) In the current release you did the right thing to correct the invalid lib architectures= in the library.properties. That should then build okay so please provide the build output so we can see more clearly what is being found (or not) 3) You say the STL is missing from the Visual Micro library manager. We use the same library.index.json that Arduino IDE uses, it is updated/downloaded from the arduino web site when you click «Check for updates» in the Visual Micro library manager. It’s possible the library has only recently been added to the Library Manager by Arduino? I will check the initial .json file that is installed with visual micro to ensure it too is up to date for new users. |
||
|
« Last Edit: Jul 27th, 2019 at 2:58pm by Tim@Visual Micro » |
||
|
WWW
|
|
yet-another-average-joe
Junior Member Posts: 27 Joined: Jul 27th, 2019 |
Re: [BUG] Weird compilation issue with ArduinoSTL library (C++ templates) |
Print Post |
|
Thanks for your answer. What I meant is that the library does not appear in «add library» (it can be seen in «download and install Arduino library») : there is no «#3) issue». Sorry, english is not my first langage ! Here’s the error report (templates are the plague when it comes to debugging The errors with the comparison function when compiling TestSTL_1.ino ; no errors with Arduino IDE. Code (C++)
TestSTL.ino: 4:14: error: 'Item' was not declared in this scope
Error compiling project sources
Build failed for project 'TestSTL'
TestSTL.ino: 4:20: error: 'item1' was not declared in this scope
TestSTL.ino: 4:27: error: 'Item' was not declared in this scope
TestSTL.ino: 4:33: error: 'item2' was not declared in this scope
TestSTL.ino: 4:38: error: expression list treated as compound expression in initializer [-fpermissive]
TestSTL.ino: In constructor Item::Item(uint32_t, std::string)
TestSTL.ino: 9:11: warning: 'Item::index' will be initialized after [-Wreorder]
uint32_t index
TestSTL.ino: 8:14: warning: 'std::string Item::str' [-Wreorder]
std*: string str
TestSTL.ino: 6:2: warning: when initialized here [-Wreorder]
Item(uint32_t i, std*: string s): index(i), str(s) {}
TestSTL.ino: In function bool compare(Item&, Item&)
TestSTL.ino: 26:38: error: 'bool compare(Item&, Item&)' redeclared as different kind of symbol
bool compare(Item& item1, Item& item2)
TestSTL.ino: 4:6: error: previous declaration of 'bool compare
vector:25: In file included from
string:25: from
locale:22: from
ios:22: from
serstream:49: from
ArduinoSTL.h:12: from
TestSTL.ino:1: from
algorithm: In instantiation of void std::stable_sort(RandomAccessIterator, RandomAccessIterator, Compare) [with RandomAccessIterator = Item*; Compare = bool]
algorithm:861: required from void std sort(RandomAccessIterator, RandomAccessIterator, Compare) [with RandomAccessIterator = Item*; Compare = bool]
TestSTL.ino:49: required from here
algorithm:849: error comp cannot be used as a function
if( comp( *temp, *(temp-1) ) ){
|
||
|
|
|
Tim@Visual Micro Administrator Posts: 11684 Joined: Apr 10th, 2010 |
Re: [BUG] Weird compilation issue with ArduinoSTL library (C++ templates) |
Print Post |
|
1) What I meant is that the library does not appear in «add library» In library manager you can download libraries for ANY platform/architecture. In the Arduino IDE you can add the #includes manually and the library might or might not compile depending on if it it designed for your architecture or not. Also in the Arduino IDE the menu notice that there is menu «Sketch>Include Library» Your library does not appear in this menu because the architectures= specification does not allow it. Changing to architectures=* show the library on «Sketch>Include Library» in the Arduino IDE. This means that a library existing in Library Manager is unrelated to the library being considered valid or not. In fact if you had two libraries installed that contained the same header.h the Arduino IDE would not know which is the correct library, therefore having accurate «architecture=» is quite important. Arduino IDE allows «invalid» libraries to be compiled, the next release of Visual Micro does the same but with some extra logic. 2) Pleae provide the information requested in the yellow box at the top of this page as a .txt file. You can email the .txt or click the Reply button here and attach. Please follow the guide and switch on «Verbose» and «Show build properties» as shown in the yellow box. Thanks |
||
|
WWW
|
|
yet-another-average-joe
Junior Member Posts: 27 Joined: Jul 27th, 2019 |
Re: [BUG] Weird compilation issue with ArduinoSTL library (C++ templates) |
Print Post |
|
I always use these options : — Verbose I copied the output to a txt file : [EDIT] when downloaded the txt file is not readable ! |
||
|
« Last Edit: Jul 27th, 2019 at 3:49pm by yet-another-average-joe » |
||
|
Please Register or Login to the Forum to see File Attachments |
||
|
|
|
Tim@Visual Micro Administrator Posts: 11684 Joined: Apr 10th, 2010 |
Re: [BUG] Weird compilation issue with ArduinoSTL library (C++ templates) |
Print Post |
|
The links you posted are ok, they give the link to the files. 1) 2) Please attach or email your .ino code and I will try to post example of adding prototypes which are simple like this… Code (C++) //create a user type
struct a ....
//prototype of method using user type
foo(a);
//method using user type
foo(a)
{
}
The arduino ide, not so long ago, was improved to make better judgment about the location of the prototypes it inserts into .ino code in background during build. It’s not 100% solution but better than the old way that Visual Micro uses. We will be changing to the new system during the next few months. An alternative is to always put the creation of user types in .h files (or .cpp) then use them in your .ino code. |
||
|
WWW
|
|
yet-another-average-joe
Junior Member Posts: 27 Joined: Jul 27th, 2019 |
Re: [BUG] Weird compilation issue with ArduinoSTL library (C++ templates) |
Print Post |
|
Not sure I understand what you are asking for : I uploaded sources in the OP… This being said, I re-wrote the code, as you suggested, with a h and a cpp file. And now it compiles fine with the comprison function. See attachment. But IMHO, it does not really solve the problem. vMicro should behave like Arduino IDE compiler, shouldn’t it ? This being said, the code that does not works is somewhat stupid. The comparison function HAS to be a member of the objects to be compared. |
||
|
Please Register or Login to the Forum to see File Attachments |
||
|
|
|
Tim@Visual Micro Administrator Posts: 11684 Joined: Apr 10th, 2010 |
Re: [BUG] Weird compilation issue with ArduinoSTL library (C++ templates) |
Print Post |
|
Oh yes, sorry I forgot that you uploaded sources initially. I will take a look |
||
|
WWW
|
|
Tim@Visual Micro Administrator Posts: 11684 Joined: Apr 10th, 2010 |
Re: [BUG] Weird compilation issue with ArduinoSTL library (C++ templates) |
Print Post |
|
Ignore my post about prototypes you are using cpp/h anyway. I have the same compile issue with STM32 in the Arduino IDE as I do in Visual Micro. The library only appears to work with avr and samd boards. In the Arduino IDE, please switch on «file>preferences>compile versbose». Then compile the same sketch and post the output as .txt Thanks |
||
|
WWW
|
|
yet-another-average-joe
Junior Member Posts: 27 Joined: Jul 27th, 2019 |
Re: [BUG] Weird compilation issue with ArduinoSTL library (C++ templates) |
Print Post |
|
(I’m always verbose !) Please let me know what sources you want me to compile… TestSTL_1 or the last sources (with a separate Item class/vector definition) Discussing with ArduinoSTL author, it appears that ArduinoSTL is not designed for STM32, as you suggested… I hope I’m not making people losing their time… Where I live, it’s 7:30PM. Saturday. Don’t you think you should be having a beer or whatever ? (on my side, the wife will soon be shouting on me…). Weekend is coming, and I will be abroad, with no MC, in a few days (tuesday), for 2 weeks. |
||
|
|
|
Tim@Visual Micro Administrator Posts: 11684 Joined: Apr 10th, 2010 |
Re: [BUG] Weird compilation issue with ArduinoSTL library (C++ templates) |
Print Post |
|
Thanks, yes I agree that library can not work with STM32. The confusion is because Arduino allows you to try to build libraries that sometimes are not valid. Visual Micro did not allow this, which in some ways is good but to avoid confusion we will do same as Arduino now Enjoy your beer. |
||
|
« Last Edit: Jul 27th, 2019 at 5:38pm by Tim@Visual Micro » |
||
|
WWW
|
|
yet-another-average-joe
Junior Member Posts: 27 Joined: Jul 27th, 2019 |
Re: [BUG] Weird compilation issue with ArduinoSTL library (C++ templates) |
Print Post |
|
Great ! |
||
|
|

Сообщение было отмечено Sasha119121 как решение



IP Logged

