The code :
#include <bits/stdc++.h>
using namespace std;
int main()
{
cout << "this file works";
return 0;
}
Compiler output was ridiculously long. Some 114000 characters long. This is only a portion of it.
Compiler output on compilation :
C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:174:5: note: no known conversion for argument 1 from 'std::basic_string_view<wchar_t>' to 'std::filesystem::__cxx11::path::string_type&&' {aka 'std::__cxx11::basic_string<wchar_t>&&'}
C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:167:5: note: candidate: 'std::filesystem::__cxx11::path::path(std::filesystem::__cxx11::path&&)'
path(path&& __p) noexcept
^~~~
C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:167:5: note: no known conversion for argument 1 from 'std::basic_string_view<wchar_t>' to 'std::filesystem::__cxx11::path&&'
C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:165:5: note: candidate: 'std::filesystem::__cxx11::path::path(const std::filesystem::__cxx11::path&)'
path(const path& __p) = default;
^~~~
C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:165:5: note: no known conversion for argument 1 from 'std::basic_string_view<wchar_t>' to 'const std::filesystem::__cxx11::path&'
C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:163:5: note: candidate: 'std::filesystem::__cxx11::path::path()'
path() noexcept { }
^~~~
C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:163:5: note: candidate expects 0 arguments, 1 provided
The terminal process terminated with exit code: 1
Terminal will be reused by tasks, press any key to close it.
So, what you are seeing is an error message, not an output.
If the file bits/stdc++.h is not a file that you created,
then what you need to do is to replace
#include <bits/stdc++.h>
with
#include <iostream>
and that's gonna solve it for you.
Related
I'm using Clang 14 (on Apple M1), which has full support for C++ 17, and I'm trying to utilize the new to_chars function. Here's my very simple test file:
#include <charconv>
#include <iostream>
int main() {
char a[10];
double pi = 3.141592;
std::to_chars_result res = std::to_chars(a, a+10, pi);
*res.ptr = '\0';
std::cout << a << std::endl;
}
My compile command is clang -std=c++17 test_to_chars.cpp, and the output is below:
test_to_chars.cpp:8:30: error: call to deleted function 'to_chars'
std::to_chars_result res = std::to_chars(a, a+10, pi);
^~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/charconv:166:6: note: candidate function has been explicitly deleted
void to_chars(char*, char*, bool, int = 10) = delete;
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/charconv:450:1: note: candidate template ignored: requirement 'is_integral<double>::value' was not satisfied [with _Tp = double]
to_chars(char* __first, char* __last, _Tp __value)
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/charconv:458:1: note: candidate function template not viable: requires 4 arguments, but 3 were provided
to_chars(char* __first, char* __last, _Tp __value, int __base)
^
test_to_chars.cpp:8:24: error: no viable conversion from 'void' to 'std::to_chars_result'
std::to_chars_result res = std::to_chars(a, a+10, pi);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/charconv:154:25: note: candidate constructor (the implicit copy constructor) not viable: cannot convert argument of incomplete type 'void' to 'const std::to_chars_result &' for 1st argument
struct _LIBCPP_TYPE_VIS to_chars_result
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/charconv:154:25: note: candidate constructor (the implicit move constructor) not viable: cannot convert argument of incomplete type 'void' to 'std::to_chars_result &&' for 1st argument
2 errors generated.
I'm calling to_chars(char*, char*, double) but for some reason it's using an implicit conversion and trying to call to_chars(char*, char*, bool, int = 10) instead, which is a deleted function.
Is there a way for me to tell C++ that I don't want it to convert my double parameter to a bool?
I'm using Clang 14 (on Apple M1), which has full support for C++ 17
This is unfortunately not correct. While the compiler itself has full C++17 support, the stdlib of your clang version (Apple clang 14) does not implement any floating point charconv features.
See the entry "Elementary string conversions" in the cppreference table.
It is important to note that you are not running "clang 14", but "Apple clang 14". Your code snippet compiles just fine on normal clang 14.
This question already has answers here:
Difference in make_shared and normal shared_ptr in C++
(8 answers)
Closed 3 months ago.
I got this error :
error: no matching function for call to 'Company::Company(Company*)'
145 | noexcept(noexcept(::new((void )__p)
| ^~~~~~~~~~~~~~~~~~ 146 | _Up(std::forward<_Args>(__args)...)))
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Test.cpp:20:5: note: candidate: 'Company::Company(int, int)' 20 | Company(int
companyId,int value):companyId(companyId),value(value){
| ^~~~~~~ Test.cpp:20:5: note: candidate expects 2 arguments, 1 provided Test.cpp:9:7: note: candidate: 'constexpr
Company::Company(const Company&)'
9 | class Company{
| ^~~~~~~ Test.cpp:9:7: note: no known conversion for argument 1 from 'Company' to 'const Company&' Test.cpp:9:7: note:
candidate: 'constexpr Company::Company(Company&&)' Test.cpp:9:7: note:
no known conversion for argument 1 from 'Company*' to 'Company&&'
The shared pointer gives me an error, I have no idea what the reason is, any idea please?
Code:
#include <iostream>
#include <string>
#include <memory>
using namespace std;
class Company{
public:
int companyId;
int value;
int HighestSalaryEmployee;
int highestSalary;
int LowestIDHighestSalaryEmployee;
int lowestSalary;
int numEmployees;
public:
Company(int companyId,int value):companyId(companyId),value(value){
HighestSalaryEmployee=11;
highestSalary=0;
LowestIDHighestSalaryEmployee=112;
numEmployees=0;
}
};
int main()
{
shared_ptr<Company> ptr1 = make_shared<Company>(new Company(123456,100));
}
This has nothing to do with noexcept and everything to do with using std::make_shared correctly. The way std::make_shared works is similar to how various emplace functions work: You pass the arguments used to construct the specified type, and it constructs them.
So what the error message is trying to tell you is that it's attempting to construct a Company out of a Company*, and Company doesn't have any constructors that go from a Company* to a Company.
The solution is to drop the new Company(...) and simply pass the arguments directly:
std::shared_ptr<Company> ptr1 = std::make_shared<Company>(123456,100);
I know that similar questions have been asked already, but I could not find the answer by looking at similar posts. Here is a minimal working example of my problem with the following C++ code:
#include <iostream>
#include <cstdio>
#include <fstream>
using namespace std;
class File{
public:
fstream value;
string name;
unsigned int number_of_lines;
};
void print_filename(File file){
cout << "Name of file is " << file.name << "\n";
}
int main(void){
File file;
print_filename(file);
cout << "\n";
return(0);
}
When I compile, I get the error:
example.cpp: In function ‘int main()’:
example.cpp:28:22: error: use of deleted function ‘File::File(const File&)’
print_filename(file);
^
example.cpp:7:7: note: ‘File::File(const File&)’ is implicitly deleted because the default definition would be ill-formed:
class File{
^~~~
example.cpp:7:7: error: use of deleted function ‘std::basic_fstream<_CharT, _Traits>::basic_fstream(const std::basic_fstream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]’
In file included from example.cpp:3:0:
/usr/local/include/c++/7.2.0/fstream:925:7: note: declared here
basic_fstream(const basic_fstream&) = delete;
^~~~~~~~~~~~~
example.cpp:18:6: note: initializing argument 1 of ‘void print_filename(File)’
void print_filename(File file){
^~~~~~~~~~~~~~
Do you know why?
Thank you for your help
Being able to read an error is a valuable skill! Let's do it.
error: use of deleted function ‘File::File(const File&)’
You are calling File's copy constructor, which doesn't exist.
note: ‘File::File(const File&)’ is implicitly deleted
The compiler has implicitly chosen to forbid copy construction of File.
error: use of deleted function ‘basic_fstream(const std::basic_fstream&)
It's because a copy constructor would need fstream's copy constructor, which has been deleted.
note: declared here
basic_fstream(const basic_fstream&) = delete;
^~~~~~~~~~~~~
That's the code that explicitly states that copy construction is not allowed.
note: initializing argument 1 of ‘void print_filename(File)’
void print_filename(File file){
Here is where the problem exists in your code.
The solution, as commented, is to not make a copy. It's not needed.
Pass by reference instead.
I'm trying to compile the following code:
#include <boost/optional.hpp>
void foo(boost::optional<unsigned> x = boost::none);
placed in the file a.cu, with the CUDA compiler, using the following command line:
nvcc a.cu -c --std=c++11 -I/opt/boost/include
but I get a bunch of errors:
a.cu:2:53: error: conversion from ‘const boost::none_t(boost::none_t::init_tag (*)())’ to ‘boost::optional<unsigned int>’ is ambiguous
void foo(boost::optional<unsigned> x = boost::none);
^
/opt/boost/include/boost/optional/optional.hpp:805:1: note: candidate: boost::optional<T>::optional(boost::optional<T>::rval_reference_type) [with T = unsigned int; boost::optional<T>::rval_reference_type = unsigned int&&] <near match>
optional ( rval_reference_type val ) : base( boost::forward<T>(val) )
^ ~~~~
/opt/boost/include/boost/optional/optional.hpp:805:1: note: conversion of argument 1 would be ill-formed:
a.cu:2:53: error: invalid conversion from ‘const boost::none_t (*)(boost::none_t::init_tag (*)())’ to ‘unsigned int’ [-fpermissive]
void foo(boost::optional<unsigned> x = boost::none);
^
/opt/boost/include/boost/optional/optional.hpp:800:1: note: candidate: boost::optional<T>::optional(boost::optional<T>::argument_type) [with T = unsigned int; boost::optional<T>::argument_type = const unsigned int&] <near match>
optional ( argument_type val ) : base(val) {}
^ ~~~~
/opt/boost/include/boost/optional/optional.hpp:800:1: note: conversion of argument 1 would be ill-formed:
a.cu:2:53: error: invalid conversion from ‘const boost::none_t (*)(boost::none_t::init_tag (*)())’ to ‘unsigned int’ [-fpermissive]
void foo(boost::optional<unsigned> x = boost::none);
Why does this happen, and can I circumvent the problem while still actually using boost::optional in (host-side) code compiled with nvcc?
Additional information:
The code compiles fine with g++ 6.3.0 (my distribution's compiler).
This code (or rather, similar code) used to compile and work on an earlier Linux distribution I was using, where the compiler was g++ 5.4.x .
I've tried this with Boost versions 1.65.1 and 1.69.0 .
I've tried this with CUDA versions 9.2.88 and 10.0.130 .
I had the exact same error and was able to get this to work with this modification:
#define BOOST_OPTIONAL_USE_OLD_DEFINITION_OF_NONE
#include <boost/optional.hpp>
This is using CUDA 10.0.130, g++ 7.3.0, and Boost 1.68.0.
A partial answer to the second question:
You could consider using Andrzej Krzemieński's neat and self-contained implementation of an optional instead of boost::optional. It works with C++11, which is what you seem to be doing.
I'm trying to convert a character from a c string to an int but I keep running into an error.
Here's my code
while(std::getline(file, line)){
if(std::isdigit(line[0]) && std::isspace(line[1]) && std::isdigit(line[2])){
SequenceArray.push_back(line);
if(std::stoi(line[2])== (SequenceArray.size() -1)){
std::cout<< "Success" << std::endl;
The error that I keep getting is as follows:
a1.cpp: In function ‘int main(int, char**)’:
a1.cpp:30:25: error: call of overloaded ‘stoi(char&)’ is ambiguous
if(std::stoi(line[2])== (SequenceArray.size() -1)){
^
a1.cpp:30:25: note: candidates are:
In file included from /usr/include/c++/4.8/string:52:0,
from /usr/include/c++/4.8/bits/locale_classes.h:40,
from /usr/include/c++/4.8/bits/ios_base.h:41,
from /usr/include/c++/4.8/ios:42,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from a1.cpp:1:
/usr/include/c++/4.8/bits/basic_string.h:2823:3: note: int std::stoi(const string&, std::size_t*, int) <near match>
stoi(const string& __str, size_t* __idx = 0, int __base = 10)
^
/usr/include/c++/4.8/bits/basic_string.h:2823:3: note: no known conversion for argument 1 from ‘char’ to ‘const string& {aka const std::basic_string<char>&}’
/usr/include/c++/4.8/bits/basic_string.h:2926:3: note: int std::stoi(const wstring&, std::size_t*, int) <near match>
stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
^
/usr/include/c++/4.8/bits/basic_string.h:2926:3: note: no known conversion for argument 1 from ‘char’ to ‘const wstring& {aka const std::basic_string<wchar_t>&}’
a1.cpp:35:6: warning: label ‘std’ defined but not used [-Wunused-label]
std:exit(EXIT_FAILURE);
A char implicit converts to a int, you don't need to use extra functions.
'a' = 97, 'b' = 98, 'c'=99, etc., following the ASCII table
So if you write,
char a_char = 'a';
int a_val = a_char;
cout << a_val << endl;
you have:
97
For std::stoi missing, try #include <string> (and enable C++11). However see also this thread - the Windows ports of g++ have had a long-standing issue with support of stoi and to_string.
The second error is that std:exit should be std::exit.
The third error is because of line[2].c_str(). You have not told us what line is but the error message suggests it is a std::string. So line[2] is a char and char does not have any member functions. If you explain what you are trying to do in the code std::atoi(line[2].c_str()) someone will be able to help. Maybe you meant line[2] - '0' which will give an integer between 0 and 9 if the third character in the line was a digit.
std::stoi() is C++11. Not all compilers enable C++11 by default.
The first error is because you haven't enabled C++11 support. GCC currently chooses C++03 by default, and stoi didn't exist in that version.
Add -std=c++11 to the compiler's arguments. If that doesn't work, try -std=c++0x, and think about getting a more up-to-date compiler. If you're stuck with an ancient compiler, then use atoi as in the code you originally posted (or perhaps something involving strtol, if you want to detect errors).
Also make sure you've included <string> for the declaration of that function.
The second error is because you wrote : instead of ::.