Error: no known conversion for - c++

So this is what I currently have, with the Scanner provided by my professor.
#include "Similarity.h"
#include "Scanner.h"
using namespace std;
int Similarity::readData(Scanner inFile){
int similarityInputSize;
vector< vector<int> > containingVector;
bool nextValueFound;
similarityInputSize = inFile.nextInt();
int lineCount = 0;
while(inFile.hasNext()){
containingVector.push_back(vector<int>());
for(int i = 0; i < similarityInputSize; i++){
containingVector[lineCount].push_back(inFile.nextInt());
}
lineCount++;
}
for(int i = 0; i < containingVector.size(); i++){
for(int j = 0; j < similarityInputSize; i++){
cout << containingVector[i][j] << " ";
}
cout << endl;
}
return 0;
}
The Main class is given to us and involves a call by value not by reference, which my professor got to work.
the errors I receive are:
In file included from /usr/include/c++/4.7/ios:43:0,
from /usr/include/c++/4.7/ostream:40,
from /usr/include/c++/4.7/iostream:40,
from ../../Utilities/Utils.h:17,
from Main.h:11:
/usr/include/c++/4.7/bits/ios_base.h: In copy constructor ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’:
/usr/include/c++/4.7/bits/ios_base.h:788:5: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
In file included from /usr/include/c++/4.7/ios:45:0,
from /usr/include/c++/4.7/ostream:40,
from /usr/include/c++/4.7/iostream:40,
from ../../Utilities/Utils.h:17,
from Main.h:11:
/usr/include/c++/4.7/bits/basic_ios.h:64:11: error: within this context
In file included from ../../Utilities/Utils.h:18:0,
from Main.h:11:
/usr/include/c++/4.7/fstream: In copy constructor ‘std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)’:
/usr/include/c++/4.7/fstream:420:11: note: synthesized method ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’ first required here
In file included from /usr/include/c++/4.7/ios:44:0,
from /usr/include/c++/4.7/ostream:40,
from /usr/include/c++/4.7/iostream:40,
from ../../Utilities/Utils.h:17,
from Main.h:11:
/usr/include/c++/4.7/streambuf: In copy constructor ‘std::basic_filebuf<char>::basic_filebuf(const std::basic_filebuf<char>&)’:
/usr/include/c++/4.7/streambuf:800:7: error: ‘std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const __streambuf_type&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_streambuf<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]’ is private
In file included from ../../Utilities/Utils.h:18:0,
from Main.h:11:
/usr/include/c++/4.7/fstream:69:11: error: within this context
/usr/include/c++/4.7/fstream: In copy constructor ‘std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)’:
/usr/include/c++/4.7/fstream:420:11: note: synthesized method ‘std::basic_filebuf<char>::basic_filebuf(const std::basic_filebuf<char>&)’ first required here
In file included from Main.h:12:0:
../../Utilities/Scanner.h: In copy constructor ‘Scanner::Scanner(const Scanner&)’:
../../Utilities/Scanner.h:27:7: note: synthesized method ‘std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)’ first required here
Main.cpp: In function ‘int main(int, char**)’:
Main.cpp:58:31: note: synthesized method ‘Scanner::Scanner(const Scanner&)’ first required here
In file included from Main.h:14:0:
Similarity.h:23:9: error: initializing argument 1 of ‘int Similarity::readData(Scanner)’
So I don't quite understand what happened. Everywhere I looked said that the issue would be fixed by a call by reference not by value. However, his provided code does not include any call by references, which makes me assume its a more subtle error (or its so big that it's blinding me). Nevertheless, I changed it anyways and it gave me these errors:
Similarity.cpp:12:5: error: prototype for ‘int Similarity::readData(Scanner&)’ does not match any in class ‘Similarity’
Similarity.h:23:9: error: candidate is: int Similarity::readData(Scanner)
Main.cpp: In function ‘int main(int, char**)’:
Main.cpp:58:32: error: no matching function for call to ‘Similarity::readData(Scanner*)’
Main.cpp:58:32: note: candidate is:
In file included from Main.h:14:0:
Similarity.h:23:9: note: int Similarity::readData(Scanner)
Similarity.h:23:9: note: no known conversion for argument 1 from ‘Scanner*’ to ‘Scanner’
Anything you can tell me at all will be a help.
Thanks in advance!

I'm guessing Scanner isn't meant to be copied. The syntax:
int Similarity::readData(Scanner inFile){
implies that Scanner will be copied from the passed in variable to the variable received by this method via Scanner's copy constructor.
Scanner sounds like something that deals with iostreams under the hood. Streams in the iostreams library do not have copy constructors.
I'm not entirely clear how you tried to pass by reference, but you need to change the signature in this file and the corresponding .h file to:
int Similarity::readData(Scanner& inFile){
And you should have no problem. readData now receives a reference to the passed in file. This is essentially an alias of the thing passed in -- anything you do to it will be reflected on the object that was passed in.

Related

Compilation error 'is implicitly deleted because the default definition would be ill-formed'

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.

namespace std and LLVM PassManagerInternal.h file

I'm exploring compiler writing with Flex, Bison and LLVM (latest versions of all) using this example: http://gnuu.org/2009/09/18/writing-your-own-toy-compiler/
Github source is on the last page of that link. I can't get it to compile and any help would be appreciated.
Unfortunately for me, the solution is non-obvious.
Here are the errors:
In file included from codegen.cpp:2:
In file included from ./codegen.h:8:
In file included from /usr/local/Cellar/llvm/3.8.1/include/llvm/IR/PassManager.h:46:
/usr/local/Cellar/llvm/3.8.1/include/llvm/IR/PassManagerInternal.h:298:12: error: use of undeclared identifier 'make_unique'
return make_unique<ResultModelT>(Pass.run(IR, AM));
^
/usr/local/Cellar/llvm/3.8.1/include/llvm/IR/PassManagerInternal.h:298:24: error: unexpected type name 'ResultModelT': expected expression
return make_unique<ResultModelT>(Pass.run(IR, AM));
^
/usr/local/Cellar/llvm/3.8.1/include/llvm/IR/PassManagerInternal.h:336:12: error: use of undeclared identifier 'make_unique'
return make_unique<ResultModelT>(Pass.run(IR));
^
/usr/local/Cellar/llvm/3.8.1/include/llvm/IR/PassManagerInternal.h:336:24: error: unexpected type name 'ResultModelT': expected expression
return make_unique<ResultModelT>(Pass.run(IR));
^
codegen.cpp:36:24: error: no matching conversion for functional-style cast from 'unique_ptr<llvm::Module>' to 'llvm::EngineBuilder'
ExecutionEngine *ee = EngineBuilder( unique_ptr<Module>(module) ).create();
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/anaconda/include/llvm/ExecutionEngine/ExecutionEngine.h:493:7: note: candidate constructor (the implicit move constructor) not viable: no known conversion
from 'unique_ptr<llvm::Module>' to 'llvm::EngineBuilder' for 1st argument
class EngineBuilder {
^
/anaconda/include/llvm/ExecutionEngine/ExecutionEngine.h:493:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion
from 'unique_ptr<llvm::Module>' to 'const llvm::EngineBuilder' for 1st argument
/anaconda/include/llvm/ExecutionEngine/ExecutionEngine.h:525:3: note: candidate constructor not viable: no known conversion from 'unique_ptr<llvm::Module>'
to 'llvm::Module *' for 1st argument
EngineBuilder(Module *m) : M(m) {
^
codegen.cpp:131:49: warning: expression with side effects will be evaluated despite being used as an operand to 'typeid'
[-Wpotentially-evaluated-expression]
std::cout << "Generating code for " << typeid(**it).name() << endl;
^
In file included from codegen.cpp:2:
In file included from ./codegen.h:8:
/usr/local/Cellar/llvm/3.8.1/include/llvm/IR/PassManager.h:207:22: error: no member named 'getName' in 'llvm::Module'
<< IR.getName() << "\n";
~~ ^
codegen.cpp:30:5: note: in instantiation of member function 'llvm::PassManager<llvm::Module>::run' requested here
pm.run(*module);
^
In file included from codegen.cpp:2:
In file included from ./codegen.h:8:
/usr/local/Cellar/llvm/3.8.1/include/llvm/IR/PassManager.h:517:20: error: no member named 'getName' in 'llvm::Module'
<< IR.getName() << "\n";
~~ ^
/usr/local/Cellar/llvm/3.8.1/include/llvm/IR/PassManager.h:372:28: note: in instantiation of member function
'llvm::AnalysisManager<llvm::Module>::invalidateImpl' requested here
return derived_this()->invalidateImpl(IR, std::move(PA));
^
/usr/local/Cellar/llvm/3.8.1/include/llvm/IR/PassManager.h:217:22: note: in instantiation of member function
'llvm::detail::AnalysisManagerBase<llvm::AnalysisManager<llvm::Module>, llvm::Module>::invalidate' requested here
PassPA = AM->invalidate(IR, std::move(PassPA));
^
codegen.cpp:30:5: note: in instantiation of member function 'llvm::PassManager<llvm::Module>::run' requested here
pm.run(*module);
^
Well, there is llvm::make_unique in include/llvm/ADT/STLExtras.h. You have not stated if you are writing your own code based on llvm source or compiling llvm or what but I assume llvm::make_unique is there exactly for that reason to fill the missing pieces of C++11.
Moreover, looking at the tutorials it seems that llvm::make_unique is explicitly used everywhere.
Update
So I successfully build the given example against LLVM 3.9.0 from svn. I had to change and fix all the calls to getGlobalContext() since that is gone in LLVM 3.9.0, but I did not encounter any make_unique related issues. However the example itself is a bit broken. codegen.h is missing an include guard, codegen.cpp is missing an include to #include "llvm/IR/LLVMContext.h", even if it wants to use the old getGlobalContext() method. I did this on Windows with MSVC 2015, can you specify your environment and compiler so that I can try the same?

Long, nearly incoherent errors C++

Sometimes I get incredibly long errors in my code that I don't understand so I just rework my code to avoid whatever was causing the error. I had another one today that I simply can't avoid.
My code:
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <vector>
using namespace std;
void readFile(string);
class info {
public:
int rows;
int cols;
vector < string > data;
};
int main(int argc, char **argv){
string filename1;
filename = argv[1];
readFile(filename);
return 0;
}
//should read onle line at a time from a file and print it
void readFile(string filename1){
fstream datafile;
datafile.open(filename1);
while (!datafile.eof()){
string line;
getline(datafile,line);
cout<<line<<endl;
}
datafile.close();
}
The error stems from trying to get the name of the file from argv[1]. It was working fine when I just gave it the file name.
The error:
project2.cpp: In function ‘int main(int, char**)’:
project2.cpp:22:2: error: ‘filename’ was not declared in this scope
filename = argv[1];
^
project2.cpp: In function ‘void readFile(std::string)’:
project2.cpp:32:25: error: no matching function for call to ‘std::basic_fstream<char>::open(std::string&)’
datafile.open(filename1);
^
project2.cpp:32:25: note: candidate is:
In file included from project2.cpp:2:0:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/fstream:889:7: note: void std::basic_fstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
open(const char* __s,
^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/fstream:889:7: note: no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const char*’
I am using Cygwin. I used it last semester as well when I was writing code in C, and my professor had us check certain installation options at the time. Could these installation options be the root of the problem? Or are errors like this common in C++? Thanks.
Just read the error:
project2.cpp: In function ‘int main(int, char**)’: project2.cpp:22:2:
error: ‘filename’ was not declared in this scope filename = argv[1];
^
Here it says that filename is not declared. i.e. You have to declare it or something wrong with the declaration
Looking at the code you have
string filename1;
One assumes you meant
string filename;
Fix this error - then try again
The first error:change filename1 to filename
The second error: you should set a open()functions in the class info.then you can use it

new c++11 for loop causes: "error: ‘begin’ was not declared in this scope"

I'm trying to learn c++, so I wrote a short program that uses the new c++11 for loop, which makes the compiler give me an error I don't understand.
this is my c++ code:
#include <iostream>
#include <cmath>
using namespace std;
float legge_oraria_moto_accelerato(float a[3]){
return a[2]*a[0] + 0.5*a[1]*a[0]*a[0];
}
int corri(float (f)(float array[3]), float arrays[3][3])
{ for(auto i:arrays) cout << f(i) << '\n';
return 0;
}
int main()
{
return 0;
}
and this is the compiler's (g++ -std=gnu++11) error:
mezzo.cpp: In function ‘int corri(float (*)(float*), float (*)[3])’:
mezzo.cpp:9:18: error: ‘begin’ was not declared in this scope
{ for(auto i:arrays) cout << f(i) << '\n';
^
mezzo.cpp:9:18: note: suggested alternatives:
In file included from /usr/include/c++/4.9/bits/basic_string.h:42:0,
from /usr/include/c++/4.9/string:52,
from /usr/include/c++/4.9/bits/locale_classes.h:40,
from /usr/include/c++/4.9/bits/ios_base.h:41,
from /usr/include/c++/4.9/ios:42,
from /usr/include/c++/4.9/ostream:38,
from /usr/include/c++/4.9/iostream:39,
from mezzo.cpp:1:
/usr/include/c++/4.9/initializer_list:89:5: note: ‘std::begin’
begin(initializer_list<_Tp> __ils) noexcept
^
/usr/include/c++/4.9/initializer_list:89:5: note: ‘std::begin’
mezzo.cpp:9:18: error: ‘end’ was not declared in this scope
{ for(auto i:arrays) cout << f(i) << '\n';
^
mezzo.cpp:9:18: note: suggested alternatives:
In file included from /usr/include/c++/4.9/bits/basic_string.h:42:0,
from /usr/include/c++/4.9/string:52,
from /usr/include/c++/4.9/bits/locale_classes.h:40,
from /usr/include/c++/4.9/bits/ios_base.h:41,
from /usr/include/c++/4.9/ios:42,
from /usr/include/c++/4.9/ostream:38,
from /usr/include/c++/4.9/iostream:39,
from mezzo.cpp:1:
/usr/include/c++/4.9/initializer_list:99:5: note: ‘std::end’
end(initializer_list<_Tp> __ils) noexcept
^
/usr/include/c++/4.9/initializer_list:99:5: note: ‘std::end’
Range-based for loops work with arrays, but not with pointers. The issue here is that arrays is actually a pointer and not an array.
When you have a function parameter that is declared as an array, it is adjusted to a pointer type. You can see this here with the parameter float arrays[3][3]: In the compiler error message you can see that the actual parameter type is a pointer to an array float (*)[3], which can't be used with a ranged-based for loop.
If you pass the array by reference instead (float (&arrays)[3][3]), it won't adjusted to a pointer in this manner and will therefore work with the range-based for loop.

Boost: Threading and mutexes in a functor

I'm trying something simple with threads and mutexes in C++ with boost.
This is the code:
#include <iostream>
#include <boost/thread/thread.hpp>
class mutex_test
{
private:
boost::mutex mut;
public:
void operator()()
{
boost::mutex::scoped_lock lock(mut);
std::cout << "Hi!" << std::endl;
}
};
int main()
{
mutex_test tester;
boost::thread tester_thread(tester);
tester_thread.join();
return 0;
}
On compile, I get this error:
In file included from C:\boost/boost/thread/detail/thread.hpp:15:0,
from C:\boost/boost/thread/thread.hpp:22,
from main.cpp:3:
C:\boost/boost/thread/detail/move.hpp: In instantiation of 'typename boost::decay<T>::type boost::thread_detail::decay_copy(T&&) [with T = mutex_test&; typename boost::decay<T>::type = mutex_test]':
C:\boost/boost/thread/detail/thread.hpp:265:88: required from 'boost::thread::thread(F&&) [with F = mutex_test&]'
main.cpp:20:36: required from here
C:\boost/boost/thread/detail/move.hpp:246:37: error: use of deleted function 'mutex_test::mutex_test(const mutex_test&)'
main.cpp:5:7: note: 'mutex_test::mutex_test(const mutex_test&)' is implicitly deleted because the default definition would be ill-formed:
main.cpp:5:7: error: use of deleted function 'boost::mutex::mutex(const boost::mutex&)'
In file included from C:\boost/boost/thread/mutex.hpp:14:0,
from C:\boost/boost/thread/detail/thread.hpp:16,
from C:\boost/boost/thread/thread.hpp:22,
from main.cpp:3:
C:\boost/boost/thread/win32/mutex.hpp:29:9: error: declared here
In file included from C:\boost/boost/thread/thread.hpp:22:0,
from main.cpp:3:
C:\boost/boost/thread/detail/thread.hpp: In instantiation of 'boost::detail::thread_data<F>::thread_data(F&&) [with F = mutex_test]':
C:\boost/boost/thread/win32/thread_heap_alloc.hpp:100:72: required from 'T* boost::detail::heap_new(A1&&) [with T = boost::detail::thread_data<mutex_test>; A1 = mutex_test]'
C:\boost/boost/thread/detail/thread.hpp:214:38: required from 'static boost::detail::thread_data_ptr boost::thread::make_thread_info(F&&) [with F = mutex_test; boost::detail::thread_data_ptr = boost::intrusive_ptr<boost::detail::thread_data_base>]'
C:\boost/boost/thread/detail/thread.hpp:265:88: required from 'boost::thread::thread(F&&) [with F = mutex_test&]'
main.cpp:20:36: required from here
C:\boost/boost/thread/detail/thread.hpp:98:40: error: use of deleted function 'mutex_test::mutex_test(const mutex_test&)'
In file included from C:\boost/boost/system/system_error.hpp:14:0,
from C:\boost/boost/thread/exceptions.hpp:22,
from C:\boost/boost/thread/win32/thread_primitives.hpp:16,
from C:\boost/boost/thread/win32/thread_data.hpp:11,
from C:\boost/boost/thread/thread.hpp:15,
from main.cpp:3:
C:\boost/boost/system/error_code.hpp:214:36: warning: 'boost::system::posix_category' defined but not used [-Wunused-variable]
C:\boost/boost/system/error_code.hpp:215:36: warning: 'boost::system::errno_ecat' defined but not used [-Wunused-variable]
C:\boost/boost/system/error_code.hpp:216:36: warning: 'boost::system::native_ecat' defined but not used [-Wunused-variable]
Makefile:21: recipe for target 'main.o' failed
The error only occurs when I try to create a thread for the functor; executing it straight works fine.
Additionally, if I remove all references to mutexes and locks it works fine.
E: Had the wrong error log. Whoops.
boost::mutex is not a copyable or moveable (using boost's move implementation) type, and hence, passing it in that way to the thread will not work, since that constructor makes a copy of the functor to execute on the thread.
In your test case, you would want to use the following
boost::thread tester_thread(boost::ref(functor))
This passes it by reference, instead of by copy. Note, this also requires that the functor remain valid until the thread exits. Since you're joining the thread, that's the case here, but more complicated cases that might not be true.