Thread: xy was not declared in this scope: compiler error!

  1. #1
    registered citizen twilight's Avatar
    Join Date
    Aug 2009
    Location
    Germany
    Posts
    23

    xy was not declared in this scope: compiler error!

    Hey everybody,

    I finished lesson 10 today, fileio, and thought "Hey, let's combine all I have learned before.

    So I programmed a simple menu with switchcase, and thought I would give the user the choice between writing something into a file, read a file and print the content out, append something to a file or exit.
    But when I wanted to test my first function (i.e. write something to a file) I get the following errors and can't quite figure it out :-( I bet it is quite simple!!

    Any help is highly apreciated :-)

    Code:
    ~ thommy$ g++ switchfileio.cpp -o littletest
    switchfileio.h: In function ‘void write_file()’:
    switchfileio.h:15: error: ‘cout’ was not declared in this scope
    switchfileio.h:16: error: ‘cin’ was not declared in this scope
    switchfileio.h:21: error: ‘ofstream’ was not declared in this scope
    switchfileio.h:21: error: expected `;' before ‘a_file’
    switchfileio.h:22: error: ‘a_file’ was not declared in this scope
    switchfileio.h: In function ‘void read_file()’:
    switchfileio.h:28: error: ‘cout’ was not declared in this scope
    switchfileio.h: In function ‘void appendto_file()’:
    switchfileio.h:32: error: ‘cout’ was not declared in this scope
    :~ thommy$
    This is what I got so far:

    File: switchfileio.cpp

    Code:
    #include <iostream>
    #include <fstream>
    #include "switchfileio.h"
    
    using namespace std;
    
    int main(){
    	int input;		// need this one for the switch-case-menu
    
    	do {			// this is the menu, it works, have tried this with another lesson!
    		cout<<"1. Write something to a file\n";
    		cout<<"2. Read a file and print its content\n";
    		cout<<"3. Append a string to an existing file\n";
    		cout<<"4.Exit\n";
    		cin>>input;		
    		switch (input) {
    			case 1:
    				write_file();
    				break;
    			case 2:
    				read_file();
    				break;
    			case 3:
    				appendto_file();
    				break;
    			case 4:
    				cout << "Thanks for playing with me\n";
    				break;
    			default:
    				cout<<"Error\n";
    				break;
    		}
    	} while (input == 1 || input == 2 || input == 3 || input == 4);  // this is to redraw the menu when one operation is done
    		
    	
    	cin.get();
    }
    File: switchfileio.h

    Code:
    void write_file(){			// my function that is called when the user wants to write a file
    	char string[256];		// this is the string the user can enter
    	char filename[15];		// this string is used to determine the filename of the file
    	cout << "Please enter your text!\n";
    	cin >> string;
    	cout << "You entered: " << string <<"\n";	// show the user what he has written
    	cout << "Please enter a filename: ";		// ask for the filename
    	cin >> filename;
    	
    	ofstream a_file (filename);		// creates an instance of ofstream and opens a file with the content of filename
    	a_file<<string;					// write the content of "string" into the file
    	a_file.close();					// close the file
    	cout << "file " << filename <<" is written!\n";  // give success message to the user
    }
    
    void read_file(){
    	cout << "you just loaded\n";
    }
    
    void appendto_file(){
    	cout << "sorry, MP is not ready yet\n";
    }
    Last edited by twilight; 08-07-2009 at 03:23 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The include simply pastes the contents of the file and replaces the include row. So that contents if placed above the using namespace directive, causing all your errors.
    But, aside from that, here are a couple of suggestions for you:
    1) It's not usually a good idea to inline functions in headers. They are usually the place for declarations.
    2) It's not a good idea to use using directives in headers either. So use the std:: prefix in them.
    3) Use std::string and std::getline instead of char.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    Code:
    #include "switchfileio.h"
    
    using namespace std;
    The using namespace is after the included header, either place before the header include or move into the header.

  4. #4
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    Do you know what does 'scope' mean? And what about function prototypes? Use std::cout instead and add prototypes to your cpp. Like this:

    Code:
    void read_file();
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Prototypes goes into headers. Put the header code in the .cpp file instead.
    There might not even be a need for a header in this code since it's a single source file.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    registered citizen twilight's Avatar
    Join Date
    Aug 2009
    Location
    Germany
    Posts
    23
    ok everybody, thanks a lot for your quick answers.

    Putting the code from the .h file under the "using" did the trick. Thanks!!

    As I said: I am going by the tutorial and I am at lesson 10. And so I am quite bound to what I know from that 10 lessosns so far:

    Elysia:

    1.: Functions in headers: Lesson learned, functions don't go into the header! The reason why I did that was that I thought I'd only put the main() into the main-source file, and hav everything else in other files.
    2.: Using directives in the header: That is the way the tutorial does it, right now I have no clue what they mean. That was not covered in the tutorial yet. It just said "do it that way", so I did. I had no idea for example that I could use std::cout instead of "using namespace std;" and then just cout.
    3.: std::string and std::getline was not covered yet, either. The tutorial said it will come a little later. The lesson about strings just said "we are using the C-style strings for now, i.e. char string[50];

    GL.Sam:

    quite frankly, I have no idea about "scope" in that context. I understand the word, but have no idea about what it means in c++ or programming itself, for that matter.

    The tutorial said something about prototypes, but did not say where they would go nor where I would write the actual function. It even said: We are not using prototypes for now.

    Elysia: Prototypes goes into headers? So I would say:

    Code:
    #include <whatever>
    
    void write_file();
    
    main()...
    and where would I really define the function then?

    Thanks again for all answers :-) I really like communities, and hopefully this one will become one where I can give something back somewhen. In others I do, in this area (programming) I feel like a total loser :-D

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    In another cpp file.

    Every cpp needs to be compiled into an object file, and then they all need to be linked to produce the final binary. Fortunately, GCC does all that for you.

    Say you have the 3 functions in util.cpp.
    Code:
    g++ switchfileio.cpp util.cpp -o littletest
    You may also want to use inclusion guards (google it).

  8. #8
    registered citizen twilight's Avatar
    Join Date
    Aug 2009
    Location
    Germany
    Posts
    23
    thanks.

    so my guess was right: I prototype the function in the header, and then I use it in my main as I whish, but having it actually defined in another .cpp-file

    I just googled include guards, seems to be a good thing. It will be covered in the tutorial as last lessons, hence the tutorial does not make use of self-written header files yet. I just did because I thought it would be fun to try things besides the tutorial :-)

    I guess I will continue to work on my little test-program with those tipps in mind and try to get it wo actually work! but not today :-(

    thx!
    Last edited by twilight; 08-07-2009 at 05:50 AM.

  9. #9
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    #include <whatever>

    void write_file();

    main()...
    Thats not the header file, the head file would be whatever.h. Also, < > brackets tells the compiler to look in the global space, if you are just adding you own header, you should use #include "whatever.h" as this tells the compiler to look in the current directory of your program.

    Also, a header would be like this -
    Code:
    //whatever .h
    #ifndef _WHATEVER_H
    #define _WHATEVER_H
    
    void do_something();
    
    #endif
    And then the cpp as follows
    Code:
    //whatever.cpp
    #include "whatever.h"
    
    void do_something()
    {
    //do something
    }
    and finally in main.

    Code:
    #include "whatever.h"
    
    int main()
    {
      do_something();
      return 0;
    }
    Spidey out!

  10. #10
    registered citizen twilight's Avatar
    Join Date
    Aug 2009
    Location
    Germany
    Posts
    23
    thx Spidey,

    that really helped me to understand what goes where. What you first said I already knew, but maybe I said it wrong, but the rest was something I definitly needed :-)

    And as Elysia told me, I would use std::getline and std::string instead of my char-thing.

    When I tried to compile my my program with the new code in function "write_file()" in util.cpp, I get some other errors. I am mainly confused about the "20: error: cin was not declared in this scope". That is line 20, which is my call std::getline (std::cin,filename); which I took from the cplusplus-reference.

    Code:
    thommy$ g++ switchfileio.cpp util.cpp -o test
    util.cpp: In function ‘void write_file()’:
    util.cpp:20: error: ‘cin’ was not declared in this scope
    util.cpp:27: error: no matching function for call to ‘std::basic_ofstream<char, 
    std::char_traits<char> >::basic_ofstream(std::string&)’
    /usr/include/c++/4.0.0/fstream:574: note: candidates are: std::basic_ofstream
    <_CharT, _Traits>::basic_ofstream(const char*, 
    std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
    /usr/include/c++/4.0.0/fstream:558: note:  std::basic_ofstream<_CharT, _Traits>::basic_ofstream() 
    [with _CharT = char, _Traits = std::char_traits<char>]
    /usr/include/c++/4.0.0/iosfwd:92: note:  std::basic_ofstream<char, std::char_traits<char> >
    ::basic_ofstream(const std::basic_ofstream<char, std::char_traits<char> >&)
    thommy$
    So I now have 3 files:

    util.h:

    Code:
    #ifndef _UTIL_H
    #define _UTIL_H
    
    // prototyping the functions here, they are declared in util.cpp
    
    void write_file(); 
    void read_file();
    void appendto_file();
    
    #endif
    util.cpp:

    Code:
    #include <iostream>
    #include <fstream>
    #include "util.h"
    
    void write_file(){			// my function that is called when the user wants to write a file
    		
    	std::string text;
    	std::string filename;
    	
    	std::cout<<"Please enter your text, end with pressing 'Enter'\n";
    	std::getline (std::cin,text);
    	std::cout <<"You entered: " << text << " \n";
    	
    	std::cout<<"Please enter a filename to name the file your text is saved to: \n";
    	std::getline (std::cin,filename);
    	std::cout <<"You entered: " << filename << " \n";
    	
    	std::ofstream file (filename);
    	file<<text;
    	file.close();
    	
    	std::cout <<"your file " << filename << "was written successfully.\n";
    }
    
    void read_file(){
    	std::cout << "sorry, feature has yet to be coded...\n";
    }
    
    void appendto_file(){
    	std::cout << "coding in progress... please hold the line...\n";
    }
    and finally, switchfileio.cpp:

    Code:
    #include <iostream>
    #include <fstream>
    #include "util.h"
    
    int main(){
    	int input;		// need this one for the switch-case-menu
    
    	do {			// this is the menu, it works, have tried this with another lesson!
    
    		std::cout<<"1. Write something to a file\n";
    		std::cout<<"2. Read a file and print its content\n";
    		std::cout<<"3. Append a string to an existing file\n";
    		std::cout<<"4.Exit\n";
    		std::cin>>input;		
    		switch (input) {
    			case 1:
    				write_file();
    				break;
    			case 2:
    				read_file();
    				break;
    			case 3:
    				appendto_file();
    				break;
    			case 4:
    				std::cout << "Thanks for playing with me\n";
    				break;
    			default:
    				std::cout<<"Error\n";
    				break;
    		}
    	} while (input == 1 || input == 2 || input == 3);  // this is to redraw the menu when one operation is done		
    	
    	std::cin.get();
    }
    Last edited by twilight; 08-07-2009 at 10:25 AM.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The ofstream constructor takes a C style string, not a std::string.
    Code:
    std::ofstream file (filename.c_str());
    bit∙hub [bit-huhb] n. A source and destination for information.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by twilight View Post
    thx Spidey,

    that really helped me to understand what goes where. What you first said I already knew, but maybe I said it wrong, but the rest was something I definitly needed :-)

    And as Elysia told me, I would use std::getline and std::string instead of my char-thing.

    When I tried to compile my my program with the new code in function "write_file()" in util.cpp, I get some other errors. I am mainly confused about the "20: error: cin was not declared in this scope". That is line 20, which is my call std::getline (std::cin,filename); which I took from the cplusplus-reference.
    Are you this is the code you compiled?
    Except for the missing <string> header and using a C-style string to open the file (as bithub points out), the code compiles fine.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    registered citizen twilight's Avatar
    Join Date
    Aug 2009
    Location
    Germany
    Posts
    23
    yes, I am sure that is the code that got compiled. :-)

    So my strings are in fact objects, right? And the c_str(); method from my file gives back a classic c-string?!

    Changed the code acordingly, and now it compiles without error, although it did not do
    what I wanted it to correctly.

    Now I fixed two "bugs" in the code, and guess what: My program actually writes all the text I entered into the file and saves the file acording to the name I entered. Woohoo :-)

    I also had to add an cin.ignore(); in my main file so that the enter from chosing the options in the menu would not skip the text-enter-part. First I wanted to have it in the write_file() method, but then I thought I would need it in my read_file(); method as well, so I think it is better of after the input of the menu-option!

    thx guys :-)

    Code:
    void write_file(){	// my function that is called when the user wants to write a file
    		
    	std::string text;
    	std::string filename;
    
    	std::cout<<"Please enter your text, end with pressing 'Enter'\n";
    	std::getline (std::cin,text);
    	std::cout <<"You entered: " << text << " \n";
    	
    	std::cout<<"Please enter a filename to name the file your text is saved to: \n";
    	std::getline (std::cin,filename);
    	std::cout <<"You entered: " << filename << " \n";
    	
    	std::ofstream file (filename.c_str());
    	file<<text.c_str();
    	file.close();
    	
    	std::cout <<"your file " << filename << " was written successfully.\n";
    }

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by twilight View Post
    So my strings are in fact objects, right? And the c_str(); method from my file gives back a classic c-string?!
    Yes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    >> So my strings are in fact objects, right?
    Correct.

    >> And the c_str(); method from my file gives back a classic c-string?!
    Yes.
    bit∙hub [bit-huhb] n. A source and destination for information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM