0

Is there any way to fix these errors and use template functions ? Or do I have to overload my functions?

It will be great if it is legal to use template functions in this case, however it seems like it is somehow difficult and problematic to use them, as I'm not sure where to declare templates (out of my class or inside it). Now I put it inside the class, but still there's some error : BinarySearch::solve no matching overloaded function found By the way, I'm using Microsoft Visual Studio. https://code.sololearn.com/cRj09n36DPHS/?ref=app

18th Mar 2023, 1:51 PM
Ali_combination
Ali_combination - avatar
22 Réponses
+ 4
Ali_combination I suggest you to go through template again and create new code again. Because how much you try to remove errors they will raise again I think code needs to be verified again. Still if you don't want to create new code then provide details of your code what is the expected output?
18th Mar 2023, 5:22 PM
R🍁🇮🇳
R🍁🇮🇳 - avatar
+ 4
Ali_combination I I hope this resource will solve your issue https://www.softwaretestinghelp.com/cpp-errors/
19th Mar 2023, 8:18 AM
R🍁🇮🇳
R🍁🇮🇳 - avatar
18th Mar 2023, 2:03 PM
R🍁🇮🇳
R🍁🇮🇳 - avatar
+ 3
Ali_combination Ok, I think I tracked down the issue with Visual Studio giving you the squiggly red warnings. In Visual Studio, you also have to add the type when instancing the class if you used template. I modified my codes above. basically, in main() you have to add a <T> after the class name. BinarySearch<int> solution(tgt , el)
20th Mar 2023, 4:41 AM
Bob_Li
Bob_Li - avatar
+ 2
https://code.sololearn.com/c0S56Ky8dXbx/?ref=app This works here , hope it helps
19th Mar 2023, 3:58 AM
Andrew Moses
Andrew Moses - avatar
+ 2
Ali_combination How about this? I reformatted it a bit so it looked cleaner and more compact on the phone, where I was playing with it. I also modified how it was used in main() 2nd code where the verbose output is made into a method. https://code.sololearn.com/cU45V00GE4Z1/?ref=app https://code.sololearn.com/cRX2D52k12mo/?ref=app
19th Mar 2023, 4:39 PM
Bob_Li
Bob_Li - avatar
+ 2
There must be a problem with your Visual Studio setup. Setting up Visual Studio can be painful. Try compiling a hello world program first to see if it can do that simple task without any problem. I rarely code in c++. But if I do, I just use SublimeText and compile it using g++ The code also runs in CompilerExplorer https://godbolt.org/ I can get it to compile in my machine with g++ (open the cmd terminal cd to folder with the cpp file) g++ -o test verboseOutput.cpp (and the program compiles) test (and the test.exe runs fine.)
20th Mar 2023, 1:00 AM
Bob_Li
Bob_Li - avatar
+ 2
I opened a new c++ console app project in Visual Studio, copy-pasted the code and it did not have any errors and was built without any problems. I would definitely say either there was a problem with your Visual Studio setup or you might have to upgrade the program
20th Mar 2023, 4:13 AM
Bob_Li
Bob_Li - avatar
+ 2
MVS sometimes require additional syntax for intellisense to function properly. Additional syntax, plus complicated setup and file structures are some of the reasons why I avoid using MVS if I was just playing with a simple C++ experiment. So many files just to create a simple .exe will clutter up my computer rapidly.
20th Mar 2023, 7:50 AM
Bob_Li
Bob_Li - avatar
+ 1
R🌸🇮🇳 Almost fixed, but still some other errors left ;( https://code.sololearn.com/ce95a43pI5qU/?ref=app No idea what it is getting at, it says : ')' before identifier 'target'
18th Mar 2023, 2:50 PM
Ali_combination
Ali_combination - avatar
+ 1
There should be constructor at line 12, not a regular function returning `T` The sequence in which private members are declared in class definition better follow the sequence in which constructor initialization works. If constructor initialize <target> before <elements>, then <target> declaration should be before declaration of <elements> inside the `private:` block. There is no need of template for main() function, just remove it.
18th Mar 2023, 3:55 PM
Ipang
+ 1
Ipang well, the IDE gave me several errors in a row, and that made me go into any possible way and try each one in order to fix the errors. Removing the template above the main function results in "argument list for class template "BinarySearch" is missing" error. Also, there's such an error "unresolved external symbol ... (?invoke_main@@YAHXZ)", unless if I replace T with the name of my ctor (BinarySearch). I thought I should give my object a template argument, as I'm using a template function. Thank you. MO ELomari
18th Mar 2023, 4:11 PM
Ali_combination
Ali_combination - avatar
+ 1
Ali_combination My suggestion to remove template definition for main() was based on SoloLearn's compiler, it worked there. If Visual Studio wants the main() to be a template function then it's their policy : )
18th Mar 2023, 4:22 PM
Ipang
+ 1
Ipang I see ^^ but unfortunately my problem is still not solved in Visual Studio (however the code created by MO ELomari works properly and without errors )
18th Mar 2023, 4:30 PM
Ali_combination
Ali_combination - avatar
+ 1
Ali_combination Sorry I don't have Visual Stidio, so I don't think I could help on that part. What problem is there in Visual Studio though? maybe someone who uses it reads this post. Perhaps they can say something ...
18th Mar 2023, 5:19 PM
Ipang
+ 1
Ipang The complete error message goes like this : unresolved external symbol main referenced in function "int__cdecl invoke_main(void)" (?invoke_main@@YAHXZ). I copied the same code bit MO ELomari sent to me, and then pasted to the IDE, but it gave me this error.
18th Mar 2023, 5:47 PM
Ali_combination
Ali_combination - avatar
+ 1
Ali_combination Post a new question, with that error message in Description. I hope someone can help you out soon.
18th Mar 2023, 6:02 PM
Ipang
+ 1
Ipang Yep, thanks friend.
18th Mar 2023, 6:09 PM
Ali_combination
Ali_combination - avatar
+ 1
Andrew Moses thanks, but sadly it only works here, not in my IDE (Microsoft Visual Studio)
19th Mar 2023, 6:22 AM
Ali_combination
Ali_combination - avatar