CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: compling error

  1. #1
    Join Date
    May 2014
    Posts
    7

    Post compling error

    Hi guys!

    I am working on a small project which contains three files
    1. MainTest.cpp
    2. CashRegister.h
    3. CashRegister.cpp

    I have done everything that suppose to be done but still I am getting an error and can not execute the program.... can anyone points out what is wrong with my program?

    Here is the code

    Code:
    // MainTest.cpp
    
    #include <iomanip>
    	using std::setw;
    	using std::left;
    #include <iostream>
    	using std::cout;
    	using std::endl;
    	using std::ios;
    #include "CashRegister.h"
    
    const char ERROR[] = "Error! Registration failed."; 
    
    int main()
    {
    	CashRegister cash("Register.txt", 5);
    
    	cout << "This test should give five error messages below!" << endl;
    	cout << "------------------------------------------------" << endl;
    
    	if (!cash.registerItem( 3,"Tvål Sun", 18.00 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 1,"Äppelmos Bob", 14.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 1,"Coca Cola 2 lit.",  12.00 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 1,"Kroppkakor 4 st.", 18.00 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 1,"Mjölk mellan 1 lit", 6.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 2,"Skottkärran Emil",  389.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 2,"Räfsan Gunda", 110.00 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 1,"Socker bit ½ kg", 14.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 3,"Tandkräm Linnéa",  9.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 3,"Deodorant Axe", 37.00 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 4,"Aftonbladet", 8.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 4,"Mitt livs korsord",  39.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 1,"Svenssons äppelpaj", 30.00 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 4,"Skraplott Lyckans Ost", 25.00 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 5,"Räfflade dyckert 1000 st.",  89.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 5,"Hammaren Adam", 65.00 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 1,"Sörens Grova Limpa", 12.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 1,"Kaffe Gevalia Mörk",  29.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 1,"Glass Holland Eis", 32.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 2,"Myrgift Sudda Bort", 82.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 5,"Sandpapper blandat set",  32.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 3,"Munvatten Dentovit", 31.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 5,"Borrmaskinen Herbert", 310.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 1,"Makrillfiléer ½ kg",  38.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 6,"Golvlampan Hilda", 230.00 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 0,"Öl Pripps Blå 6-pack", 30.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 6,"Rakapparaten Orvar",  99.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 1,"Findus Köttfärssås 2,5 dl.", 16.00 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 0,"Skruv 4x22 1000 st.", 80.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem(-1,"Blå Castello  100 gr.",  19.50 ))
    		cout << ERROR << endl << endl;
    
    	cout.setf(ios::fixed);
    	cout.setf(ios::showpoint);
    	cout.precision(2);
    	cout << setw(30) << left << "The total for this sail was:" << cash.batchTotal() << " SEK" << endl;
    	cout << setw(30) << left << "Expected result should be: " << "1473.00 SEK" << endl << endl;
    
    	return 0;
    }
    Code:
    //CashRegister.cpp
    class CashRegister{
    public:
    	CashRegister(char* fileName, int nrOfCategories);// The file contains
    	~CashRegister(); // registered items and balances
    
    	bool registerItem(int category, char* articleName, double amount);
    	double batchTotal(){
    		double total = _batchTotal;
    		_batchTotal = 0;	// batchTotal() is expected to reset _batchTotal;
    		return total;
    	}
    private:
    	ofstream _file; // Here will registered items be saved
    	int _nrOfCategories; // products are organized in categories
    	double* _categorySums; // The vector will contain respective sum of each category sold during the day
    	double _batchTotal; // sold since the last summation
    };
    Code:
    // CashRegister.h
    #ifndef CASHREGISTER_H
    #define CASHREGISTER_H
    
    	CashRegister(char* fileName, int nrOfCategories);
    	bool registerItem(int category, char* articleName, double amount);
        double batchTotal();
    
    #endif
    any kind of help will be appreciated... thanks in advance

    Sincere Regards

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: compling error

    What type of error? Compile, link, run time? What is the error?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: compling error

    Code:
    ofstream _file; // Here will registered items be saved
    where do you include fstream? (or ostream)

    Your CashRegister.h file makes no sense as it doesn't contain the class declaration. The CashRegister.cpp file contains the class declaration and a definition for batchTotal() but no definiton for the construtor, destructor and registerItem(). Definitions for these need to be provided.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    May 2014
    Posts
    7

    Re: compling error

    Hi guys!

    Thanks for your time and reply. I have made the changes you have suggested and moved the class declaration and definition to .h file

    and now the error I am getting is error: 'ofstream' does not name a type

    the line it is point out is:
    ofstream _file;

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: compling error

    Quote Originally Posted by new_bee View Post
    ... and now the error I am getting is error: 'ofstream' does not name a type

    the line it is point out is:
    ofstream _file;
    Well, you seem to ignore the post#3 of 2kaud!

    So where is the #include of the file where ofstream is declared?
    Victor Nijegorodov

  6. #6
    Join Date
    May 2014
    Posts
    7

    Re: compling error

    yes I have included the line on the top as such

    #include <ofstream>

    and yes it seems like it has solved the problem

    Thanks
    Last edited by new_bee; May 15th, 2014 at 10:53 AM.

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: compling error

    Then use
    Code:
    std::ofstream _file;
    or add
    Code:
    using namespace std;
    before your
    Code:
    int main()
    Victor Nijegorodov

  8. #8
    Join Date
    May 2014
    Posts
    7

    Re: compling error

    where do i need to add class declaration? in the .cpp file or .h file?

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: compling error

    A small clarification may be in order. This is a class definition that defines a class named X:
    Code:
    class X
    {
        // ...
    };
    A class definition is also a class declaration, but here is a declaration of X that is not its definition:
    Code:
    class X;
    Generally, unless it is implementation detail or if your program is so trivial such that it has no header files, a class would be defined in a header file that is then included wherever the definition of that class is needed, e.g., to define an object of the class type. The class might be forward declared in other places where the class name is required but the class definition is not needed, e.g., to declare a pointer to an object of the class type without creating an object of the class at that point.

    In your case, this means that the definition of CashRegister should be moved to CashRegister.h. You would remove the attempted declaration of CashRegister's construction (which was invalid anyway), and the declarations of registerItem and batchTotal (as non-member functions) from that header. Instead, your class definition would contain the valid member function declarations (which can also be member function definitions, if you want to define them inline for some reason).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #10
    Join Date
    May 2014
    Posts
    7

    Re: compling error

    Hi! I have done a lot of work to complete the application as i wanted but it did not work I am a Java developer and this part is required to be in c++ so i do not know how to handle things in headers

    can anyone help me to out the save the results in the Register.txt as such

    >>>> Register Opened: 2014-05-20 Tuesday at 17:45
    17:45 : Cat. 3 : 18.00 Kr. [Tvål Sun ]
    17:45 : Cat. 1 : 14.50 Kr. [Äppelmos Bob ]
    17:45 : Cat. 1 : 12.00 Kr. [Coca Cola 2 lit. ]
    17:45 : cat. 1 : 18.00 Kr. [Kroppkakor 4 St. ]
    17:45 : cat. 1 : 6.50 Kr. [Mjölk mellan 1 lit ]
    17:45 : cat. 2 : 389.50 Kr. [Skottkärran Emil ]
    .
    .
    .
    .
    .
    17:45 : Cat. 1 : 38.50 Kr. [Makrillfileer 1/2 Kg]
    17:45 : cat. 1 : 16.00 Kr. [Findus Köttfärssås 2,5 dl. ]
    >>>> Register closed: 2014-05-20 Tuesday at 17:45
    Category totals for last opening period:
    ----------------------------------------
    Category 1: 224.50 Kr.
    category 2: 582.00 Kr.
    Category 3: 96.00 Kr.
    Category 4: 73.00 Kr.
    Category 5: 497.50 Kr.
    Total sales within all categories: 1473.00 Kr.
    I have done everything just little more work to be done?

    I will appreciate any help

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: compling error

    Quote Originally Posted by new_bee View Post
    Hi! I have done a lot of work to complete the application as i wanted but it did not work I am a Java developer and this part is required to be in c++ so i do not know how to handle things in headers
    Well, now you programs not only compiles but also runs.
    What problem do you have with it or its results?
    Did you try to debug it?
    Victor Nijegorodov

  12. #12
    Join Date
    May 2014
    Posts
    7

    Re: compling error

    it is not giving the results what i want... rather it does not produce anything

    here is my progress so far

    MainTest.cpp
    Code:
    // MainTest.cpp
    #include <iomanip>
    	using std::setw;
    	using std::left;
    #include <iostream>
    	using std::cout;
    	using std::endl;
    	using std::ios;
    #include "CashRegister.h"
    
    const char ERROR[] = "Error! Registration failed.";
    
    int main()
    {
    	CashRegister cash("Register.txt", 5);
    
    	cout << "This test should give five error messages below!" << endl;
    	cout << "------------------------------------------------" << endl;
    
    	if (!cash.registerItem( 3,"Tvål Sun", 18.00 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 1,"Äppelmos Bob", 14.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 1,"Coca Cola 2 lit.",  12.00 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 1,"Kroppkakor 4 st.", 18.00 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 1,"Mjölk mellan 1 lit", 6.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 2,"Skottkärran Emil",  389.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 2,"Räfsan Gunda", 110.00 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 1,"Socker bit ½ kg", 14.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 3,"Tandkräm Linnéa",  9.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 3,"Deodorant Axe", 37.00 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 4,"Aftonbladet", 8.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 4,"Mitt livs korsord",  39.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 1,"Svenssons äppelpaj", 30.00 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 4,"Skraplott Lyckans Ost", 25.00 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 5,"Räfflade dyckert 1000 st.",  89.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 5,"Hammaren Adam", 65.00 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 1,"Sörens Grova Limpa", 12.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 1,"Kaffe Gevalia Mörk",  29.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 1,"Glass Holland Eis", 32.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 2,"Myrgift Sudda Bort", 82.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 5,"Sandpapper blandat set",  32.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 3,"Munvatten Dentovit", 31.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 5,"Borrmaskinen Herbert", 310.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 1,"Makrillfiléer ½ kg",  38.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 6,"Golvlampan Hilda", 230.00 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 0,"Öl Pripps Blå 6-pack", 30.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 6,"Rakapparaten Orvar",  99.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 1,"Findus Köttfärssås 2,5 dl.", 16.00 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem( 0,"Skruv 4x22 1000 st.", 80.50 ))
    		cout << ERROR << endl;
    	if (!cash.registerItem(-1,"Blå Castello  100 gr.",  19.50 ))
    		cout << ERROR << endl << endl;
    
    	cout.setf(ios::fixed);
    	cout.setf(ios::showpoint);
    	cout.precision(2);
    	cout << setw(30) << left << "The total for this sail was:" << cash.batchTotal() << " SEK" << endl;
    	cout << setw(30) << left << "Expected result should be: " << "1473.00 SEK" << endl << endl;
    
    	return 0;
    }
    CashRegister.h
    Code:
    //CashRegister.h
    #ifndef CASHREGISTER_H
    #define CASHREGISTER_H
    #include <fstream>
    
    class CashRegister{
    public:
    	CashRegister(char* fileName, int nrOfCategories);// The file contains registered items and balances
    	~CashRegister();
    
    	bool registerItem(int category, char* articleName, double amount);
    	double batchTotal(){
    		double total = _batchTotal;
    		_batchTotal = 0;	// batchTotal() is expected to reset _batchTotal;
    		return total;
    	}
    private:
    	std::ofstream _file; // Here will registered items be saved
    	int _nrOfCategories; // products are organized in categories
    	double* _categorySums; // The vector will contain respective sum of each category sold during the day
    	double _batchTotal; // sold since the last summation
    };
    
    #endif
    CashRegister.cpp
    Code:
    //CashRegister.cpp
    #include "CashRegister.h"
    #include <fstream>
    
    CashRegister::CashRegister(char* fileName, int nrOfCategories){
        _file.open(fileName, std::ofstream::in | std::ofstream::out | std::ofstream::app);
        _nrOfCategories = nrOfCategories;
    
    }
    
    bool registerItem(int category, char* articleName, double amount){
    }
    my CashRegister.cpp is not finished yet.. it requires some coding

  13. #13
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: compling error

    Quote Originally Posted by new_bee View Post
    it is not giving the results what i want... rather it does not produce anything
    The you have to debug your code to find the problem that it "does not produce anything".

    Quote Originally Posted by new_bee View Post
    ...
    my CashRegister.cpp is not finished yet.. it requires some coding
    Then finish it!
    Victor Nijegorodov

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: compling error

    Just giving you code a quick eyeball and not trying to really figure it out, I couldn't help but notice this function does nothing.

    Code:
    bool registerItem(int category, char* articleName, double amount){
    }
    It really shouldn't even compile.

    Your batchTotal function doesn't make any kind of sense. I don't see where you're totaling anything.

  15. #15
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: compling error

    Code:
    bool registerItem(int category, char* articleName, double amount){
    }
    as a minimum this should be
    Code:
    bool CashRegister::registerItem(int category, char* articleName, double amount){
         return false;
    }
    registerItem is part of the class CashRegister so needs to be defined as part of that class rather than as a non-class function. Also as the function is defined to return a bool then as it minimum it should at least return a valid bool value.

    rather it does not produce anything
    If it doesn't produce the output from the first two cout statements then you have serious problems.

    You do not check anywhere that the file has been successfully opened in the class constructor. IMO I would avoid calling any function in a constructor that might fail as success/failure cannot be easily be reported.

    Why are you using char arrays rather than the c++ string class?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured