Long, nearly incoherent errors C++ - 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

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.

To read fileName as commandLine argument and perform file Operation "open" in c++98 Linux

I am reading abc.cpp file which is placed under /home/documents/abc.cpp. To open file I am performing file operation open("t.open("/home/documents/abc.cpp"). where i am able to perform open operation on file.
I want to try to read file name using command line argument. so what i am trying here is in command line
./a.out abc.cpp , passing argv[1] as file name and concatenate string path + argv[1], when i compile the code i will thrown with compilation errors, how to solve this issue please help.
#include <iostream>
#include <fstream>
#include <sstream>
#include<string.h>
#include <ext/stdio_filebuf.h>
using namespace std;
int main(int argc, char *argv[])
{
ifstream t;
string path = "/home/documents/";
string file = path + argv[1];
t.open(file);
//t.open("/home/documents/abc.cpp");
string buffer;
string line;
while(t)
{
getline(t, line);
// ... Append line to buffer and go on
buffer += line;
buffer += "\n";
}
t.close();
return 0;
}
compilation error
g++ cmdLine.cpp
cmdLine.cpp: In function ‘int main(int, char**)’:
cmdLine.cpp:13:32: error: no matching function for call to ‘std::basic_ifstream<char>::open(std::string&)’
t.open(file);
^
cmdLine.cpp:13:32: note: candidate is:
In file included from cmdLine.cpp:2:0:
/usr/include/c++/4.8.2/fstream:538:7: note: void std::basic_ifstream<_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, ios_base::openmode __mode = ios_base::in)
^
/usr/include/c++/4.8.2/fstream:538:7: note: no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const char*’
t.open(file.c_str()); will solve your problem. Until C++11 the only function declaration was
void open( const char *filename,
ios_base::openmode mode = ios_base::in );
The error message informs you very clearly: no known conversion from ‘std::string’ to ‘const char*’.

How to fix string declaration error in scope

I'm trying to run an interprocess communication program but it says string is not declared in the scope as is and when I add #inlcude I get an error that says:
receiver.cpp:25:35: error: invalid conversion from ‘char*’ to ‘int’ [-fpermissive]
string temp = to_string(argv[0]);
~~~~~~^
In file included from /usr/include/c++/7/string:52:0,
from receiver.cpp:14:
/usr/include/c++/7/bits/basic_string.h:6419:3: note: candidate: std::__cxx11::string std::__cxx11::to_string(unsigned int) <near match>
to_string(unsigned __val)
^~~~~~~~~
receiver.cpp:27:26: error: cannot convert ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘const char*’ for argument ‘1’ to ‘int atoi(const char*)’
int msgid = atoi(temp) //Converts message id from string to integer
^
receiver.cpp:45:32: error: ‘some_data’ was not declared in this scope
if (msgrcv(msgid, (void *)&some_data, BUFSIZ, msg_to_receive, 0) == -1) { //revieces message from message queue
^~~~~~~~~
receiver.cpp:49:29: error: ‘some_data’ was not declared in this scope
printf("You wrote: %s", some_data.some_text);
This is my code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.H>
#include <cstring.h>
#include <unist.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <cstdlib>
#inlcude <string>
using namespace std;
struct my_msg_st{
long int my_msg_type;
char some_text[BUFSIZ];
};
int main(int argc, char *argv[0]){
int running =1;
string temp = to_string(argv[0]);
int msgid = atoi(temp);
struct my_msg_st some_data;
long int msg_to_receive = 0;
....
if (strncmp(some_data.some_text, "end", 3) == 0){
running =0;
}
...
exit(0);
}
expecting for the code to print out the message sent from the sender file
Here are some fixes for your issues:
string temp = to_string(argv[0]);
1. to_string converts numbers to string. the argv[0] is a C-style string, not a number.
2. The std::string constructor already has a version to convert from char * to std::string.
atoi(temp)
1. The atoi function takes a parameter of type char * not std::string. You'll need to use atoi(temp.c_str()) or prefer std::ostringstream.
Please review the differences between char arrays (a.k.a. C-Style strings) and the std::string type. Prefer to use std::string, especially in structures.
Carefully read the library function descriptions before using them.
See also std::ostringstream. Since this is C++, prefer to use C++ I/O such as std::cout and operator <<.

C++ Scrubbing Windows New Line Elements in a String on a Unix Compiler

I'm using a Unix shell compiler and need to import a Windows .dat file for input. Unfortunately this means there exists native '\r\n' components for carriage returns in the input file.
I'm hoping to scrub these out with something along the lines of the following:
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream inputFile;
inputFile.open("myFile.dat");
string array[100];
int i = 0;
while(getline(dataIn, str))
{
str.erase(remove(str.begin(), str.end(), '\n'), str.end());
str.erase(remove(str.begin(), str.end(), '\r'), str.end());
array[0] = str;
i++;
}
return 1;
}
However this is providing the following error:
error: cannot convert ‘__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >’ to ‘const char*’ for argument ‘1’ to ‘int remove(const char*)’
for the first erase(), followed by
error: request for member ‘erase’ in ‘temp.std::basic_string<_CharT, _Traits, _Alloc>::c_str [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]()’, which is of non-class type ‘const char*’
for the second.
I've attempted str.c_str().erase but this has resulted in duplicates of the second error. Any suggestions would be greatly appreciated...
Two problems in the code:
You if you want to use the algorithm function remove, you need to add #include <algorithm>.
To ensure the ::remove (which is a function that removes the file named by the char * argument) isn't picked up, use std::remove.

Removing duplicates string array c++

I would like to read from a file containing one column of strings, i.e.
AAAA
BBBB
22
4556
.
.
.
and rewrite in the same file only the unique elements.
sprintf(nameID,"Try.dat");
IDFile = fopen (nameID,"r+");
std::vector<std::string> test;
fputs (test,IDFile)
std::sort(test);
auto it = std::unique(std::begin(test), std::end(test));
test.erase(it, test.end());
for(int k = 0; k<test.size();k++){
fprintf (IDFile,"%s \n",test[k].c_str());
}
fclose (IDFile);
But I get the following errors
error: cannot convert ‘std::vector<std::basic_string<char> >’ to ‘const char*’ for argument ‘1’ to ‘int fputs(const char*, FILE*)’
error: no matching function for call to ‘sort(std::vector<std::basic_string<char> >&)’
warning: ‘auto’ changes meaning in C++11; please remove it [-Wc++0x-compat]
error: ‘it’ does not name a type
Any help/better way of doing it ?
thanks
The standard library is your friend:
#include <set>
#include <iostream>
int main()
{
std::set<std::string> set;
for (std::string line; getline(std::cin, line);)
set.insert(line);
for (auto const& s : set)
std::cout << s << '\n';
}