Small Problem

Pages: 12
Why this is not working? Thanks in advance, its a small problem but i dont remember how to fix it! xD

1
2
3
4
5
6
//...
   string fen4;
   cin>>fen4;
   //fen4==randomfile.txt
   ofstream myfile (fen4); //The problem
//... 
Hi,
The code is perfectly normal, as far as I can see.
Can you provide us with your full code and better describe the problem you are trying to deal with?
Sorry but i cant, there's the error's, if you really need them, say again. Thanks in advance.
1
2
3
4
5
6
7
8
9
10
11
12
||=== Build: Debug in PR Encrypt messages (compiler: GNU GCC Compiler) ===|
F:\Projects\C++\Code Blocks\Mine\PR Encrypt messages\main.cpp||In function 'int main()':|
F:\Projects\C++\Code Blocks\Mine\PR Encrypt messages\main.cpp|53|error: no matching function for call to 'std::basic_ofstream<char>::basic_ofstream(std::string&)'|
F:\Projects\C++\Code Blocks\Mine\PR Encrypt messages\main.cpp|53|note: candidates are:|
D:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\fstream|643|note: std::basic_ofstream<_CharT, _Traits>::basic_ofstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]|
D:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\fstream|643|note:   no known conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to 'const char*'|
D:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\fstream|628|note: std::basic_ofstream<_CharT, _Traits>::basic_ofstream() [with _CharT = char; _Traits = std::char_traits<char>]|
D:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\fstream|628|note:   candidate expects 0 arguments, 1 provided|
D:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\fstream|602|note: std::basic_ofstream<char>::basic_ofstream(const std::basic_ofstream<char>&)|
D:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\fstream|602|note:   no known conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to 'const std::basic_ofstream<char>&'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 2 second(s)) ===|
> If you really need them, say again.
I need them. Ok? :)
 
 //Cutted code 
Last edited on
You double-included the same header in your main.cpp
1
2
3
4
5
6
#include <iostream>
#include <string>
#include "encry loop.h"
#include <stdlib.h>
#include "decry loop.h"
#include <iostream> 
ofstream does not take a string as the filename parameter in the versions prior c++11. So you should enable C++11 in your ide. Or use c_str():

ofstream myfile (fen4.c_str());
EDIT: (After 5 hours), i didnt see +coder777 because we was posted EXACTLY at the same time :), DONT READ THIS PART PLEASE!

Same error's

There's how to:
1
2
3
4
fen = "EncryptResultText"; //Base text
ifen2= ifen2 + 1; //An number, to be diferent with other's in this sesios (unit program is closed)
fen3= ".txt"; //The file termination
fen4= fen + fen2 + fen3; //Combine them all 


The eloop and dloop are just another surce file's, no problem with then.
Last edited on
How about you declare the variable at the beginning of the function instead :
1
2
3
4
5
6
7
8
int main()
{
    ofstream myfile; // Note : it must stay here
    start:
    string fen, fen3, fen4, fen2, elr;
    int ifen2 = 0;
    fen2 = ifen2;
    string rde, d, k, w;


And in the place where you open a file, replace the line with :
myfile.open(fen4.c_str());

Cause : This problem is often caused by multiple nested go to. They may be problematic, so you should not use them.
Another error's:
1
2
3
4
5
6
7
||=== Build: Debug in PR Encrypt messages (compiler: GNU GCC Compiler) ===|
F:\Projects\C++\Code Blocks\Mine\PR Encrypt messages\main.cpp||In function 'int main()':|
F:\Projects\C++\Code Blocks\Mine\PR Encrypt messages\main.cpp|50|error: expected ')' before '{' token|
F:\Projects\C++\Code Blocks\Mine\PR Encrypt messages\main.cpp|49|error: could not convert 'myfile.std::basic_ofstream<_CharT, _Traits>::open<char, std::char_traits<char> >(fen4.std::basic_string<_CharT, _Traits, _Alloc>::c_str<char, std::char_traits<char>, std::allocator<char> >(), std::operator|((std::_Ios_Openmode)16u, (std::_Ios_Openmode)32u))' from 'void' to 'bool'|
F:\Projects\C++\Code Blocks\Mine\PR Encrypt messages\main.cpp|75|error: expected primary-expression before '}' token|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|


**I will be afk for something like 2 hours**
Last edited on
Show us the part that contains compiler errors.
You've already been given two solutions to this:

1) Use a C++11 compliant compiler (or turn on the C++11 option in your compiler).

2) Use the string::c_str() function:
// Line 50
 
            ofstream myfile (fen4.c_str());     


Get rid of the gotos. gotos are evil.
Last edited on
You try to isolate the problem. Make a copy of main.cpp. Then you try removing some random part of your main.cpp you suspect there may be errors (by using cut) and compile the code. If your code does not compile, undo the action and try it elsewhere. If your code compiles successfully, post the "malfunction" code that causes the compiler errors here.
Last edited on
Im back, there's one more error,
1
2
3
4
||=== Build: Debug in PR Encrypt messages (compiler: GNU GCC Compiler) ===|
F:\Projects\C++\Code Blocks\Mine\PR Encrypt messages\main.cpp||In function 'int main()':|
F:\Projects\C++\Code Blocks\Mine\PR Encrypt messages\main.cpp|50|error: cannot convert 'std::basic_ofstream<_CharT, _Traits>::open<char, std::char_traits<char> >' from type 'void (std::basic_ofstream<char>::)(const char*, std::ios_base::openmode) {aka void (std::basic_ofstream<char>::)(const char*, std::_Ios_Openmode)}' to type 'bool'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|


+AbstactionAnon
If i do what you said, i got 1 error too, that is:
1
2
3
4
5
||=== Build: Debug in PR Encrypt messages (compiler: GNU GCC Compiler) ===|
F:\Projects\C++\Code Blocks\Mine\PR Encrypt messages\main.cpp||In function 'int main()':|
F:\Projects\C++\Code Blocks\Mine\PR Encrypt messages\main.cpp|50|error: cannot resolve overloaded function 'open' based on conversion to type 'bool'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

Almost and we fixed it!
Also, i need to get rid of ALL goto? Or just how many i can?
Also x2, why all of us got reported?
Thanks in advance for fixes!
Last edited on
Please show the corresponding code.

Also, why all of us got reported?
Who cares
 
 //Cutted code xD 


I try dem all, i still got:
1
2
3
4
||=== Build: Debug in PR Encrypt messages (compiler: GNU GCC Compiler) ===|
F:\Projects\C++\Code Blocks\Mine\PR Encrypt messages\main.cpp||In function 'int main()':|
F:\Projects\C++\Code Blocks\Mine\PR Encrypt messages\main.cpp|48|error: could not convert 'myfile.std::basic_ofstream<_CharT, _Traits>::open<char, std::char_traits<char> >(fen4.std::basic_string<_CharT, _Traits, _Alloc>::c_str<char, std::char_traits<char>, std::allocator<char> >(), std::operator|((std::_Ios_Openmode)16u, (std::_Ios_Openmode)32u))' from 'void' to 'bool'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|


EDIT: I replaced the wrong error part, accidentaly copyed from another way, now its fixed (ONLY IN THIS REPLY!)
EDIT2: I remaked the code, cuting an non-so important part, i cutted some goto, NO MAJORLY CHANGING, Still This error.
Last edited on
Line 48: ofstream::open returns a void. You can;t test a void in an if statement. Use is_open().
http://www.cplusplus.com/reference/fstream/ofstream/open/

Line 47-48: These lines are redundant. When a filename is supplied to the ofstream constructor, it is implicitly opened. No need to open it again.

Also, i need to get rid of ALL goto? Or just how many i can?

IMO, any goto is a symptom of poorly structured code.

Last edited on
+AvstractionAnon
LINE 48: I still dont understand how to use it ..., can i hace an example?
LINE 47-48: Sorry, a typo mistake, i didnt wanted to do that, but thanks for seeing it!
I changed if (myfile.open(fen4.c_str()))
With if (myfile.open, std::ofstream::out | std::ofstream::app)

But i get new error :(
1
2
3
4
||=== Build: Debug in PR Encrypt messages (compiler: GNU GCC Compiler) ===|
F:\Projects\C++\Code Blocks\Mine\PR Encrypt messages\main.cpp||In function 'int main()':|
F:\Projects\C++\Code Blocks\Mine\PR Encrypt messages\main.cpp|48|error: left operand of comma operator cannot resolve address of overloaded function|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
The following line is wrong for several reasons:
1
2
3
4
if (myfile.open, std::ofstream::out | std::ofstream::app)
//  ^^^^^^^^^^^ - That's a function address, not a function call
//             ^ - That's a misuse of the comma operator
//               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - Those are only valid on a function call to open 


Try this:
1
2
3
           ofstream myfile (fen4.c_str());
           if (myfile.is_open()) 
           { ...


Last edited on
Pages: 12