This is the error and program. I gives me this error, and I cant find any mispelling in main.cpp
ERROR
main.cpp: In member function ‘int GPIO::unexport_gpio()’:
main.cpp:27:45: error: conversion from ‘const char*’ to non-scalar type ‘std::ofstream {aka std::basic_ofstream<char>}’ requested
ofstream unexportgpio = (unexport_str.c_str());
~~~~~~~~~~~~~~~~~~~^~~
main.cpp:28:7: error: ‘exportgpio’ was not declared in this scope
if(!(exportgpio.is_open())
^~~~~~~~~~
main.cpp:29:3: error: expected ‘)’ before ‘return’
return -1;
^~~~~~
main.cpp:30:2: error: ‘exportgpio’ was not declared in this scope
exportgpio<<this->gpionum;
^~~~~~~~~~
main.cpp: In member function ‘int GPIO::setdir_gpio(std::__cxx11::string)’:
main.cpp:36:64: warning: statement has no effect [-Wunused-value]
std::string setdir_str = "/sys/class/gpio/gpio"+this->gpionum;+"/direction";
^~~~~~~~~~~~~
main.cpp:37:41: error: conversion from ‘const char*’ to non-scalar type ‘std::ofstream {aka std::basic_ofstream<char>}’ requested
ofstream setdirgpio = (setdir_str.c_str());
~~~~~~~~~~~~~~~~~^~~
main.cpp: At global scope:
main.cpp:48:2: error: ‘setdirgpio’ does not name a type
setdirgpio.close();
^~~~~~~~~~
main.cpp:49:2: error: expected unqualified-id before ‘return’
return 0;
^~~~~~
main.cpp:50:1: error: expected declaration before ‘}’ token
}
^
main.cpp: In member function ‘int GPIO::setdir_gpio(std::__cxx11::string)’:
main.cpp:47:2: warning: control reaches end of non-void function [-Wreturn-type]
}
^
main.cpp
#include "main.h"
GPIO::GPIO()
{
this->gpionum = "4";
}
GPIO::GPIO(std::string gnum)
{
this->gpionum = gnum;
}
int GPIO::export_gpio()
{
string export_str = "/sys/class/gpio/export";
ofstream exportgpio(export_str.c_str());
if(!(exportgpio.is_open()))
return -1;
exportgpio << this->gpionum;
exportgpio.close();
return 0;
}
int GPIO::unexport_gpio()
{
std::string unexport_str = "/sys/class/gpio/unexport";
ofstream unexportgpio = (unexport_str.c_str());
if(!(exportgpio.is_open())
return -1;
exportgpio<<this->gpionum;
return 0;
}
int GPIO::setdir_gpio(std::string x)
{
std::string setdir_str = "/sys/class/gpio/gpio"+this->gpionum;+"/direction";
ofstream setdirgpio = (setdir_str.c_str());
if(!(setdirgpio.is_open()))
return -1;
if(x == "0")
{
setdirgpio<<"in";
}
if(x == "1")
setdirgpio<<"out";
}
setdirgpio.close();
return 0;
}
int GPIO::setval_gpio(std::string x)
{
string setval_str = "/sys/class/gpio/gpio"+this->gpionum+"/value";
ofstream setvalgpio = (setval_str.c_str());
if(!(setvalgpio.is_open()))
return -1;
if(x == "0")
{
setvalgpio<<"0";
setvalgpio.close();
}
if(x == "1")
setvalgpio<<"1";
setvalgpio.close();
}
return 0;
}
Im trying to make a program that switches the state on my RPI3.
Im used to it on the Arduino, but not on RPI3.
If you know how to fix, then don't hesitate to write.
It is supposed to drive my garten sprinkler
ofstream unexportgpio = (unexport_str.c_str());
should be
ofstream unexportgpio(unexport_str.c_str());
Incidentally this is an odd piece of code
std::string unexport_str = "/sys/class/gpio/unexport";
ofstream unexportgpio(unexport_str.c_str());
Why not do it the simpler way?
ofstream unexportgpio("/sys/class/gpio/unexport");
There's no need to make a std::string from a C string literal, only to get the C string back again.
Related
I have this basic multi tool program which goal is to complete four functions on a file that contains some strings.. take a string, for example, take a line and put it in uppercase.
I get that it's not perfect yet, but i need to understand why my char* and char** are doing some mess.
thanks a lot for your time
#include <iostream> //to use enter and exit
#include <cstring>
#include <string>
#include <fstream> //to use files
using namespace std;
/*--------------------------FONCTION PART-----------------------------------*/
//define one fonction for each transformation
//FONCTION 1
void fonctionu (char* argv){
char pattern = argv[2];
char file = argv[3];
fstream data(file, ios::in);
if (data){
string line;
while (getline (data ,line)){
unsigned found = line.find(pattern);
if (found != string::npos) {
for (int i = 0; line[i]!='\0'; i++) {
if(line[i] >= 'a' && line[i] <= 'z') {
line[i] = line[i] - (32);// (ASCII)
cout<<line[i];
}} }
if (found == string::npos) {
for (int i = 0; line[i]!='\0'; i++) {
cout<<line[i];
}}}}}
//FONCTION 2
void fonctiond( char* argv){ //remove line with xyz
char pattern = argv[2];
char file = argv[3];
fstream data(file, ios::in);
if (data){
string line;
while (getline (data ,line)){
unsigned found = line.find(pattern); //as a reminder argv[2] is the pattern (warning : it need to be an unsigned to compare two unsigned)
if (found != string::npos) {
//delete the line of the file when there is the pattern
cout<<'\0'; }
if (found == string::npos){
for (int i = 0; line[i]!='\0'; i++) {
cout<< line[i];
}}}}}
//FONCTION 3
void fonctionc( char* argv){
char pattern = argv[2];
char file = argv[3];
fstream data(file, ios::in);
if (data ){
string line;
while (getline (data ,line)){
unsigned found = line.find(pattern); //as a reminder argv[2] is the pattern
if (found != string::npos) {
for (int i = 0; line[i]!='\0'; i++) {
cout<< "\033[31m"<< line[i]; //the line will be red
}
}
if (found == string::npos){
for (int i = 0; line[i]!='\0'; i++) {
cout<< line[i];
}}}}}
//FONCTION 4
//replace the pattern xyz by abc
void fonctions ( char* argv){
char pattern = argv[2];
char file = argv[3];
fstream data(file, ios::in);
if (data ){
string line;
while (getline (data ,line)){
unsigned found = line.find(pattern);
if (found != string::npos) {
for (int i = 0; line[i]!='\0'; i++) {
if(line[i] >= 'a' && line[i] <= 'z') {
line[i] = line[i] - (97); // (ASCII) //we creat a shift that allow to make the x became a 'a' and the y a 'b'...etc
cout<<line[i];}}
}
if (found == string::npos) {
for (int i = 0; line[i]!='\0'; i++) {
cout<<line[i];
}}}}}
/*--------------------------MAIN PART---------------------------------------*/
int main(char* argv){
// ipsacs : argv[0]
//option :
string a = argv[1]; //string ! to compare line 103,108 and 112
// pattern : argv[2];
//file : argv[3];
if (argv[1]=='\0' || argv[2]=='\0'){
cout <<"ERROR"<<'\n';}
if (a == "u"){
char fonctionu (argv);}
if (a== "d"){
char fonctiond(argv);}
if (a == "c"){
char fonctionc(argv);}
if (a == "s"){
char fonctions(argv);}
return 0;
}
/*--------------------------TEST PART--------------------------------------*/
/*
pour compiler : g++ -Wall ipsacs.cpp -o ipsacs
./ipsacs u xyz foo //how the program would receive arguments, basically
Le programme ne renvoie rien.
*/
here is the compiling log:
main.cpp:22:30: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
fstream data(file, ios::in);
^
In file included from main.cpp:8:0:
/usr/include/c++/6/fstream:902:7: note: initializing argument 1 of ‘std::basic_fstream<_CharT, _Traits>::basic_fstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits; std::ios_base::openmode = std::_Ios_Openmode]’
basic_fstream(const char* __s,
^~~~~~~~~~~~~
main.cpp: In function ‘void fonctiond(char*)’:
main.cpp:46:29: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
fstream data(file, ios::in);
^
In file included from main.cpp:8:0:
/usr/include/c++/6/fstream:902:7: note: initializing argument 1 of ‘std::basic_fstream<_CharT, _Traits>::basic_fstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits; std::ios_base::openmode = std::_Ios_Openmode]’
basic_fstream(const char* __s,
^~~~~~~~~~~~~
main.cpp: In function ‘void fonctionc(char*)’:
main.cpp:66:29: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
fstream data(file, ios::in);
^
In file included from main.cpp:8:0:
/usr/include/c++/6/fstream:902:7: note: initializing argument 1 of ‘std::basic_fstream<_CharT, _Traits>::basic_fstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits; std::ios_base::openmode = std::_Ios_Openmode]’
basic_fstream(const char* __s,
^~~~~~~~~~~~~
main.cpp: In function ‘void fonctions(char*)’:
main.cpp:88:29: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
fstream data(file, ios::in);
^
In file included from main.cpp:8:0:
/usr/include/c++/6/fstream:902:7: note: initializing argument 1 of ‘std::basic_fstream<_CharT, _Traits>::basic_fstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits; std::ios_base::openmode = std::_Ios_Openmode]’
basic_fstream(const char* __s,
^~~~~~~~~~~~~
main.cpp: At global scope:
main.cpp:109:5: warning: first argument of ‘int main(char*)’ should be ‘int’ [-Wmain]
int main(char* argv){
^~~~
main.cpp:109:5: warning: ‘int main(char*)’ takes only zero or two arguments [-Wmain]
main.cpp: In function ‘int main(char*)’:
main.cpp:113:20: error: conversion from ‘char’ to non-scalar type ‘std::string {aka std::basic_string}’ requested
string a = argv[1]; //string ! to compare line 103,108 and 112
~~~~~~^
main.cpp:121:26: error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
char fonctionu (argv);}
^
main.cpp:123:25: error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
char fonctiond(argv);}
^
main.cpp:125:27: error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
char fonctionc(argv);}
^
main.cpp:127:27: error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
char fonctions(argv);}
^
A char * is a pointer to a memory address holding a character, and is often used for c-strings.
A char ** is a pointer to a memory address holding a pointer to a memory address holding a character, and is often used for arrays of c-strings.
Your main signature if not a valid form, since there's no form of main which takes in only a character pointer. The form you are most likely looking for is int main( int argc, int **argv ). This takes in two parameters: argc being the number of arguments passed to the program, and argv containing the parameters, as c-strings, passed to the program.
In the main method, you would generally ensure that argc is correct, i.e. you have the correct number of parameters passed into your function (note: it's always at least 1 will argv[0] being the program name).
std::string a = argv[1] is fine, and it will cause the string pointed to by argv to be converted to a string. When calling your functions, you have char functionu(argv);. You will want to remove the char from that line; effectively, the way it is written, you are trying to create a char variable named functionu which has an initial value of argv.
Each of your functions, instead of taking in a char * should take in a char ** instead, then when you have something like char file = argv[3];, you would want to change this to char *file = argv[3];, or std::string file = argv[3];
I need help fixing the following code, it is a c++ whitespace detector that uses different words to produce different outputs:
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
string Line;
string firstWord[99];
string secondWord[99];
int lineFunction(string line) {
string line[199];
for (int i = 0; i < line.length(); i++) {
while (isspace(line[i]) == false) {
firstWord += line[i];
}
secondWord += line[i];
}
}
int main() {
cin >> Line;
lineFunction(Line);
if (firstWord == "A") {
if (secondWord == "B") {
cout << "AB";
}
}
}
The expected result when I input A B (with a space) should be the letters AB (without a space, to test how it works) printed onto the screen, as a result of the if statements, but I am trying to make the output configurable. The errors that I receive when I try to run this are:
main.cpp: In function ‘int lineFunction(std::string)’:
main.cpp:12:20: error: declaration of ‘std::string line [199]’ shadows a parameter
string line[199];
^
main.cpp:13:30: error: request for member ‘length’ in ‘line’, which is of non-class type ‘std::string [199] {aka std::basic_string [199]}’
for (int i = 0; i < line.length(); i++) {
^~~~~~
main.cpp:14:31: error: no matching function for call to ‘isspace(std::string&)’
while (isspace(line[i]) == false) {
^
In file included from /usr/include/c++/6/cctype:42:0,
from main.cpp:2:
/usr/include/ctype.h:118:1: note: candidate: int isspace(int)
__exctype (isspace);
^
/usr/include/ctype.h:118:1: note: no known conversion for argument 1 from ‘std::string {aka std::basic_string}’ to ‘int’
In file included from /usr/include/c++/6/bits/basic_ios.h:37:0,
from /usr/include/c++/6/ios:44,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from main.cpp:3:
/usr/include/c++/6/bits/locale_facets.h:2565:5: note: candidate: template bool std::isspace(_CharT, const std::locale&)
isspace(_CharT __c, const locale& __loc)
^~~~~~~
/usr/include/c++/6/bits/locale_facets.h:2565:5: note: template argument deduction/substitution failed:
main.cpp:14:31: note: candidate expects 2 arguments, 1 provided
while (isspace(line[i]) == false) {
^
main.cpp:15:23: error: no match for ‘operator+=’ (operand types are ‘std::string [99] {aka std::basic_string [99]}’ and ‘std::string {aka std::basic_string}’)
firstWord += line[i];
~~~~~~~~~~^~~~~~~~~~
main.cpp:17:20: error: no match for ‘operator+=’ (operand types are ‘std::string [99] {aka std::basic_string [99]}’ and ‘std::string {aka std::basic_string}’)
secondWord += line[i];
~~~~~~~~~~~^~~~~~~~~~
main.cpp: In function ‘int main()’:
main.cpp:24:22: error: comparison between distinct pointer types ‘std::string* {aka std::basic_string*}’ and ‘const char*’ lacks a cast [-fpermissive]
if (firstWord == "A") {
^~~
main.cpp:25:27: error: comparison between distinct pointer types ‘std::string* {aka std::basic_string*}’ and ‘const char*’ lacks a cast [-fpermissive]
if (secondWord == "B") {
Here are some possible solutions to fix the errors.
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
// don't use
// using namespace std;
// don't use global variables
// string Line;
// string firstWord[99]; you need strings, not arrays
// string secondWord[99];
// use references to return multiple values and no return value
// alternatively you could return a std::pair
void lineFunction(std::string line, std::string &firstWord, std::string &secondWord) {
// string line[199]; you don't need this variable
// line.length() return unsigned int so you should compare with unsigned int
// this whole loop makes no sense
// for (unsigned int i = 0; i < line.length(); i++) {
// this would result in a infinite loop
// while (isspace(line[i]) == false) {
// if (!std::isspace(line[i])) {
// firstWord += line[i];
// }
// secondWord += line[i];
// }
unsigned int i;
for (i = 0; i < line.length() && !std::isspace(line[i]); ++i) {
firstWord += line[i];
}
for (++i; i < line.length(); ++i) {
secondWord += line[i];
}
}
int main() {
std::string line;
// std::cin only reads until first whitespace
// std::cin >> line;
std::getline(std::cin, line);
std::string firstWord;
std::string secondWord;
lineFunction(line, firstWord, secondWord);
if (firstWord == "A" && secondWord == "B") {
std::cout << "AB";
}
}
I am trying to call a void function named correction(original,corrected) and when I'm trying to compile it I get a couple of errors of the form:
error: expected primary-expression before ‘}’ token
Here is my complete code:
#include <cstdio>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
void correction(ifstream& original,ofstream& corrected);
int main ()
{
ifstream original;
ofstream corrected;
char filename[16];
cout<<"Enter filename:\n";
cin>>filename;
original.open(filename);
corrected.open("corrected.txt");
if (original.fail( ))
{
cout << "Input file opening failed.\n";
exit(1);
}
if (corrected.fail( ))
{
cout << "Input file opening failed.\n";
exit(1);
}
else
{
correction(original,corrected);
}
return 0;
}
void correction(ifstream& original,ofstream& corrected)
{
char symbol;
while(! original.eof())
{
original.get(symbol);
if (symbol== 'c')
{
corrected.put(symbol);
original.get(symbol);
if (symbol=='i')
{
corrected.put(symbol);
original.get(symbol);
if (symbol=='n')
{
corrected.put(symbol);
original.get(symbol);
while (symbol==' ')
{
original.get(symbol);
}
for (symbol=='<')
{
corrected.put('>');
original.get(symbol);
}
corrected.put(symbol);
}
}
if (symbol=='o')
{
corrected.put(symbol);
original.get(symbol);
if (symbol=='u')
{
corrected.put(symbol);
original.get(symbol);
if (symbol=='t')
{
corrected.put(symbol);
original.get(symbol);
while (symbol==' ')
{
original.get(symbol);
}
for (symbol=='>')
{
corrected.put('<');
original.get(symbol);
}
corrected.put(symbol);
}
}
}
}
else
corrected.put(symbol);
}
return;
}
When I try to compile this code I get the following errors:
operators.cpp:59:23: error: expected ‘;’ before ‘)’ token
for (symbol=='<')
^ operators.cpp:65:5: error: expected primary-expression before ‘}’ token
}
^ operators.cpp:65:5: error: expected ‘)’ before ‘}’ token > >operators.cpp:65:5: error: expected primary-expression before ‘}’
token
operators.cpp:65:5: error: expected ‘;’ before ‘}’ token
operators.cpp:83:20: error: expected ‘;’ before ‘)’ token
for (symbol=='>')
^ operators.cpp:89:9: error: expected primary-expression before ‘}’ token
}
^ operators.cpp:89:9: error: expected ‘)’ before ‘}’ token operators.cpp:89:9: error: expected primary-expression before ‘}’
token operators.cpp:89:9: error: expected ‘;’ before ‘}’ token
I am kind of new to programming and have spent a couple of hours trying to fix it but I don't understand what the problem is.
Any kind of help is greatly appreciated!
Added later after looking closely at #liup's answer:
Ouch, my bad, the OP probably indeed meant to use a loop here! Then, you can use the while loop like so:
while (symbol=='<')
and
while (symbol=='>')
Old answer:
Here:
if (symbol=='<')
and here:
if (symbol=='>')
Instead of for (symbol=='<') and for (symbol=='>').
The error stems from the fact that in a for loop, you must have all the three clauses, even if they are empty, hence two semicolons ; are obligatory. You only provided one didn't provide a single semicolon in each of the two incorrect for statements.
For reference, see for loop description at cppreference
Both for (symbol=='<') and for (symbol=='>') for loop syntax is wrong. In this scenario you can use for loop as follows.
for (; symbol == '<';)
{
corrected.put('>');
original.get(symbol);
}
for (; symbol == '>';)
{
corrected.put('<');
original.get(symbol);
}
OR
for (; symbol == '<';original.get(symbol))
{
corrected.put('>');
}
for (; symbol == '>';original.get(symbol))
{
corrected.put('<');
}
Please help. I am getting many errors.
sub2.cpp: In function ‘int main()’:
sub2.cpp:11:14: error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive]
sub2.cpp:12:14: error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive]
sub2.cpp:16:17: error: expected primary-expression before ‘const’
sub2.cpp:16:36: error: expected primary-expression before ‘const’
sub2.cpp:11:6: warning: unused variable ‘outer’ [-Wunused-variable]
sub2.cpp:12:6: warning: unused variable ‘inner’ [-Wunused-variable]
make: * [sub2] Error 1
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
char *Subtract(const char *outer, const char *inner);
int main()
{
char outer = "Bookkepper";
char inner = "keep";
char *word = new char[50];
word = Subtract(const char &outer, const char &inner);
cout << word << endl;
return 0;
}
char *Subtract(const char *outer, const char *inner)
{
int olen = strlen(outer);
int first_occ_idx = -1;
for(int i=0; i < olen; i++){
if(strncmp(outer+i, inner,strlen(inner)) == 0){
first_occ_idx = i;
}
}
if(first_occ_idx == -1){
return NULL;
}
int ilen = strlen(inner);
int xx = olen - ilen;
char *newstr = new char[xx];
int idx = 0;
for(int i=0; i < first_occ_idx; i++){
newstr[idx++] = outer[i];
}
for(int i=first_occ_idx+ilen; i < olen; i++){
newstr[idx++] = outer[i];
}
newstr[idx] = '\0';
return newstr;
}
In C++, string literals like "Bookkepper" (sic) are const character pointers, it's a little stricter than in C. So it should be:
const char *outer = "Bookkeeper"; // Note also spelling
rather than:
char outer = "Bookkepper";
In addition, you don't include types when calling a function, so:
word = Subtract(const char &outer, const char &inner);
would be better as:
word = Subtract(outer, inner);
Separately (and these are style suggestions only), the correct type for things that represent sizes (such as number of characters in a string) is size_t rather than int.
And it's usually considered good form to clean up all your dynamic memory explicitly so, before returning from main(), you could put:
delete[] word;
I was trying for the first time to read data from a file, a .txt file. Below is my code:
Level::ReadStream(std::fstream data)
{
bool write = false;
char* p = data.tellg();
while(!data.eof())
{
int i = 0, j = 0;
if(&p == '[')
{
write == true;
}
if(write == true)
{
mapX[i] = p;
mapY[j] = p;
}
if(&p == '/n')
{
i++;
j++;
}
else
{
i++;
}
*p++
}
};
void Level::SetMapSize(std::fstream data)
{
int* p = data.tellg();
int sizeX = 0;
int sizeY = 0;
while(!data.eof())
{
if(&p != '[' && &p != ']' && &p != '\n')
{
sizeX++;
}
else if(&p != '\n')
{
sizeY++;
}
}
mapX.resize(sizeX);
mapY.resize(sizeY);
std::cout << sizeX << '\n' << sizeY;
};
The goal of these two functions are to:
1- read all the chars in the file and, if the current character is not a bracket, add it to an index map(X, Y), then increase the counter so the next non-bracket value will be put in the correct index.
2- read all the file and, as before, count the non-bracket values, so it can make mapX and mapY the correct size.
However, it's not working, and I got these errors:
C:\...\Level.cpp|58|warning: multi-character character constant|
c:\...\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\ios_base.h|790|error: 'std::ios_base::ios_base(const std::ios_base&)' is private|
c:\...\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\iosfwd|47|error: within this context|
c:...\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\iosfwd|87|note: synthesized method 'std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)' first required here |
c:...\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\streambuf|770|error: 'std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]' is private|
c:...\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\iosfwd|78|error: within this context|
c:...\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\iosfwd|87|note: synthesized method 'std::basic_filebuf<char, std::char_traits<char> >::basic_filebuf(const std::basic_filebuf<char, std::char_traits<char> >&)' first required here |
C:...\Level.cpp||In constructor 'Level::Level()':|
C:...\Level.cpp|24|note: synthesized method 'std::basic_fstream<char, std::char_traits<char> >::basic_fstream(const std::basic_fstream<char, std::char_traits<char> >&)' first required here |
C:...\Level.cpp|24|error: initializing argument 1 of 'void Level::SetMapSize(std::fstream)'|
C:...\Level.cpp|29|error: 'cout' was not declared in this scope|
C:...\Level.cpp|38|error: ISO C++ forbids declaration of 'ReadStream' with no type|
C:...\Level.cpp|38|error: prototype for 'int Level::ReadStream(std::fstream)' does not match any in class 'Level'|
C :...\Level.cpp|17|error: candidate is: void Level::ReadStream(std::fstream)|
C:...\Level.cpp||In member function 'void Level::SetMapSize(std::fstream)':|
C:...\Level.cpp|75|error: invalid conversion from 'std::streamoff' to 'int*'|
C:...\Level.cpp|81|error: ISO C++ forbids comparison between pointer and integer|
C:...\Level.cpp|81|error: ISO C++ forbids comparison between pointer and integer|
C:...\Level.cpp|81|error: ISO C++ forbids comparison between pointer and integer|
C:...\Level.cpp|86|error: ISO C++ forbids comparison between pointer and integer|
C:...\Level.cpp|95|error: 'cout' is not a member of 'std'|
||=== Build finished: 15 errors, 1 warnings ===|
Can anyone help?
EDIT: Ok, I changed everything. My new code is:
void Level::ReadStream()
{
long p = textData.tellg();
int counter = 0;
int i = 0;
int j = 0;
char ch;
while(p != textData.eof())
{
textData.seekg(counter);
textData >> ch;
if(ch != '[' && ch != ']')
{
mapX[i] = ch;
mapY[j] = ch;
i++;
if(ch == '\n')
{
i = 0;
j++;
}
}
counter++;
p = textData.tellg();
}
};
with mapX and mapY being 5 int long. Now it compiles, but hangs and crashes. I don't see why... Is there anyone that can help me?
It's \n not /n! That should clear some errors.
Also, you can't just take the pointer from tellg -- it returns the position, not the pointer to the position!
Basically try to read up on IO in C++, maybe start with this question:
How to read from a file char by char in C++?