Thread: Doesn't Compile

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    11

    Doesn't Compile

    Hey guys, my family is starting a sort of in house bank and i want to write a program for it that will manage the accounts. I have started it a little bit and its not compileing. Its not much now but here is the source and the compiler log.

    Code:
    #include <iostream>
    #include <fstream>
    #include "stdafx.h"
    
    using namespace std;
    
    class Banker
    {
    public:
    	
    	const int CHECKING = 1;
    	const int SAVINGS = 0;
    
    
    	Banker(string name, int sBalance);
    	 ~Banker();
    	int GetBalance(int account);
    	int Deposit(int amount, int account);
    	int Transfer(int amount, Banker recieve);
    protected:
    	int balance;
    	string name;
    
    };
    
    int main()
    {
    	cout<< "Welcome to the Cullen Familly Banking Program.\n";
    	cout<< "Please select a choice:\n";
    	cout<< "1. Deposit";
    	cout<< "2. Check Balance";
    	cout<< "3. Make a transfer";
    	return 0;
    }
    Compiling...
    Bank Program.cpp
    C:\Cpp Stuff\Bank Program\Bank Program\Bank Program.cpp(7) : error C2871: 'std' : does not exist or is not a namespace
    C:\Cpp Stuff\Bank Program\Bank Program\Bank Program.cpp(13) : error C2258: illegal pure syntax, must be '= 0'
    C:\Cpp Stuff\Bank Program\Bank Program\Bank Program.cpp(13) : error C2252: 'CHECKING' : pure specifier can only be specified for functions
    C:\Cpp Stuff\Bank Program\Bank Program\Bank Program.cpp(14) : error C2252: 'SAVINGS' : pure specifier can only be specified for functions
    C:\Cpp Stuff\Bank Program\Bank Program\Bank Program.cpp(17) : error C2629: unexpected 'class Banker ('
    C:\Cpp Stuff\Bank Program\Bank Program\Bank Program.cpp(17) : error C2238: unexpected token(s) preceding ';'
    C:\Cpp Stuff\Bank Program\Bank Program\Bank Program.cpp(24) : error C2146: syntax error : missing ';' before identifier 'name'
    C:\Cpp Stuff\Bank Program\Bank Program\Bank Program.cpp(24) : error C2501: 'string' : missing storage-class or type specifiers
    C:\Cpp Stuff\Bank Program\Bank Program\Bank Program.cpp(24) : error C2501: 'name' : missing storage-class or type specifiers
    C:\Cpp Stuff\Bank Program\Bank Program\Bank Program.cpp(30) : error C2065: 'cout' : undeclared identifier
    C:\Cpp Stuff\Bank Program\Bank Program\Bank Program.cpp(30) : error C2297: '<<' : illegal, right operand has type 'char [48]'
    C:\Cpp Stuff\Bank Program\Bank Program\Bank Program.cpp(31) : error C2297: '<<' : illegal, right operand has type 'char [25]'
    C:\Cpp Stuff\Bank Program\Bank Program\Bank Program.cpp(32) : error C2297: '<<' : illegal, right operand has type 'char [11]'
    C:\Cpp Stuff\Bank Program\Bank Program\Bank Program.cpp(33) : error C2297: '<<' : illegal, right operand has type 'char [17]'
    C:\Cpp Stuff\Bank Program\Bank Program\Bank Program.cpp(34) : error C2297: '<<' : illegal, right operand has type 'char [19]'
    Error executing cl.exe.

    Bank Program.exe - 15 error(s), 0 warning(s)

  2. #2
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    What is your compiler? These errors are very strange to me.

    The only thing I see you must fix is the CHECKING and SAVINGS initialization. You must do it at the constructor.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    11
    Im using visual C++...but i dont understand why its not letting me use the namespace std part of it...but yeah i forgot about the initialization..thanks for that

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Set your project to not use precompiled headers and remove the stdafx.h include directive.

    MSC silently ignores everything that comes before the PCH include directive. Thus, it simply ignores your including iostream and fstream, thus has no definition of the std namespace and consequently doesn't let you use it.

    Make CHECKINGS and SAVINGS static and leave the initializations where they are.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    But static members must be initialized outside the class, donīt they?
    Code:
    class C{
       static int number;
    };
    
    int C::number = 1;
    And why make it static? Does every object will have the same value for these variables?

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Mortissus
    But static members must be initialized outside the class, donīt they?
    Code:
    class C{
       static int number;
    };
    
    int C::number = 1;
    That's for variables. For constants, it's different. In modern compilers, they can get their values inside the class. (They still need a definition outside, though.)
    Code:
    class C{
       static const int number = 1;
    };
    
    const int C::number;

    And why make it static? Does every object will have the same value for these variables?
    Given that he wanted to initialize them in the class, I'd say yes. They're constants, not variables.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    11
    How do you set it to not use precompiled headers? And what is MSC and PCH?

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    11
    Heres new code:

    Code:
    // Bank Program.cpp : Defines the entry point for the console application.
    //
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    class Banker
    {
    public:
    	
    	static const int CHECKING;
    	static const int SAVINGS;
    
    
    	Banker(string name, int sBalance);
    	 ~Banker();
    	int GetBalance(int account);
    	int Deposit(int amount, int account);
    	int Transfer(int amount, Banker recieve);
    protected:
    	int balance;
    	string name;
    
    };
    
    int main()
    {
    	cout<< "Welcome to the Cullen Familly Banking Program.\n";
    	cout<< "Please select a choice:\n";
    	cout<< "1. Deposit";
    	cout<< "2. Check Balance";
    	cout<< "3. Make a transfer";
    	return 0;
    }
    Now ive only got this error about precompiled headers...

    c:\cpp stuff\bank program\bank program\bank program.cpp(36) : fatal error C1010: unexpected end of file while looking for precompiled header directive
    Error executing cl.exe.

    Bank Program.exe - 1 error(s), 0 warning(s)

  9. #9
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Thanks CornedBee!

    Justin C, your code compiles right in gcc 3.2. Maybe a new line after the end of main()?

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Somewhere in the project options, there's a point called "precompiled headers". You need to set it to "Don't use precompiled headers."

    MSC stands for Microsoft C compiler, PCH for precompiled header.

    In the future, check the "Empty Project" box when creating a new project.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    Apr 2005
    Posts
    11
    Heres my new code..im just not using visual C++ any more cause i cant find how to turn those things off but now im using bloodshed dev C++ and heres my new code and the errors im getting with this.

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    class Banker
    {
    public:
    	
    	int CHECKING;
    	int SAVINGS;
    
    
    	Banker(string name, int sBalance);
    	 ~Banker();
    	int GetBalance(int account);
    	int Deposit(int amount, int account);
    	int Transfer(int amount, Banker recieve);
    protected:
    	int balance;
    	string name;
    
    };
    
    int main()
    {
    	cout<< "Welcome to the Cullen Familly Banking Program.\n";
    	cout<< "Please select a choice:\n";
    	cout<< "1. Deposit";
    	cout<< "2. Check Balance";
    	cout<< "3. Make a transfer";
    	return 0;
    }
    c:\cpp stuff\banking\bankingmain.cpp:15: parse error before `name'
    c:\cpp stuff\banking\bankingmain.cpp:22: syntax error before `;'

    Sorry im asking for so much but please help again

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The first might be because you didn't include <string>. The second probably as well.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Registered User
    Join Date
    Apr 2005
    Posts
    11
    Thanks alot for your patience lol, im a bit of a noob to C++, everythings working fine now, thanks =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C and C++ compile speed
    By swgh in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-02-2007, 02:37 PM
  2. Compile as you type
    By Rocketmagnet in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 12-07-2006, 01:36 PM
  3. How to compile mfc libs from platform sdk
    By tjcbs in forum Windows Programming
    Replies: 6
    Last Post: 11-19-2006, 08:20 AM
  4. Compile crashes certain windows
    By Loduwijk in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2006, 09:05 PM
  5. How can I compile C or C++ with Visual Studio .NET?
    By Dakkon in forum C Programming
    Replies: 8
    Last Post: 02-11-2003, 02:58 PM