Reputation: 11
Update: Thank you for all your responses, I did inform my professor and he fixed the issue that was on his end. Thanks again for all your help!!
I turned my class into a template, and tried to run the following code, only to get three errors.
sorter.h
#pragma once
#ifndef SORT_THINGS
#define SORT_THINGS
#include <utility>
template <typename T>
class sorter {
public:
sorter(T a,T b,T c): _a{a}, _b{b}, _c{c} {
using std::swap;
if (_a > _b) { swap (_a, _b); }
if (_b > _c) { swap (_b, _c); }
if (_a > _b) { swap (_a, _b); }
}
const T& a() const { return _a; }
const T& b() const { return _b; }
const T& c() const { return _c; }
private:
T _a, _b, _c;
};
#endif
tests.cpp (cannot edit tests.cpp file, professor created this file and locked editing on it)
#include <iostream>
#include "sorter.h"
template <typename T>
void test_sort(std::istream& in, std::ostream& out)
{
T a, b, c;
in >> a >> b >> c;
sorter<T> s {a, b, c};
out << s.a() << ' ' << s.b() << ' ' << s.c();
}
int main(int, char* argv[])
{
using namespace std;
if (argv[2] == "int"s) {
test_sort<int>(cin, cout);
} else if (argv[2] == "char"s) {
test_sort<char>(cin, cout);
} else if (argv[2] == "string"s) {
test_sort<std::string>(cin, cout);
} else {
cerr << "ungecognized test type.\n";
return 1;
}
return 0;
}
Errors:
./tests.cpp:17:20: error: unable to find string literal operator 'operator""s' with 'const char [4]', 'long unsigned int' arguments
if (argv[2] == "int"s) {
./tests.cpp:19:27: error: unable to find string literal operator 'operator""s' with 'const char [5]', 'long unsigned int' arguments
} else if (argv[2] == "char"s) {
./tests.cpp:21:27: error: unable to find string literal operator 'operator""s' with 'const char [7]', 'long unsigned int' arguments
} else if (argv[2] == "string"s) {
Question: Is there something I should change it sorter.h that will allow the code to work, without the error to reoccur?
-Sorry, for not really explaining what my problem is well.
Upvotes: 1
Views: 151
Reputation: 22152
The string literal operator that you are intending to use with the s
after the string literal e.g. here:
if (argv[2] == "int"s)
is defined in std::literals::string_literals
. It needs to be imported into the current context in order for it to be found by unqualified name lookup.
Usually this is done with a using namespace std::string_literals;
in the appropriate scope where the operator is used. Following the example of using namespace std;
in your code, it should be placed in main
:
int main(int, char* argv[])
{
using namespace std;
using namespace std::string_literals;
...
Your question indicates that you are not supposed to change main
. In that case this is a clear mistake by whoever gave you the assignment.
If you really want to make this work in this specific instance, only modifying sorter.h
, then you can also add using namespace std::string_literals;
after #include <utility>
. It is however considered bad style to put using namespace
statements in header files, since it introduces names into the global namespace of all translation units using the header file in an uncontrolled manner, and should not be done.
The one at fault here is your professor who made a simple mistake that would be easy to fix for them.
Upvotes: 3