DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2008
    Posts
    53

    ofstream within a class

    Hello,
    I'd like manage a ofstream type "inside" a class but some problem occur...If I define outfile out the class, all is ok; if I declare and define it inside the class, this happen. Any hints please?
    Code:
    static ofstream _outfile("howmany2.txt", fstream::out);
    
    class HowMany {
    	static std::ofstream outfile; 	
    	string _name;
    	static int _objectCount;
    public:
    	HowMany(const string& id = "") : _name(id) { outfile.open("howmany.txt", ios::out);  _objectCount++; }
    	static void print(const string& msg = "") {
    		if (msg.size() != 0)
    			_outfile << msg << ": ";
    		_outfile << "objectCount = "
    				<< _objectCount << endl;		
    		outfile << "hello";
    	}
    Code:
    error LNK2001: unresolved external symbol "private: static class std::basic_ofstream<char,struct std::char_traits<char> > HowMany::outfile" (?outfile@HowMany@@0V?$basic_ofstream@DU?$char_traits@D@std@@@std@@A)
    howmany.obj : error LNK2001: unresolved external symbol "private: static class std::basic_ofstream<char,struct std::char_traits<char> > HowMany::outfile" (?outfile@HowMany@@0V?$basic_ofstream@DU?$char_traits@D@std@@@std@@A)

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    You need to define the static member somewhere outside the class body:

    Code:
    class HowMany{/**/};
    std::ofstream HowMany::outfile;
    This rule applies to all non-const static data members declared inside a class.
    Danny Kalev

Similar Threads

  1. Replies: 26
    Last Post: 12-01-2012, 04:12 AM
  2. Help with Error in class
    By velkropie in forum Java
    Replies: 5
    Last Post: 03-24-2007, 11:09 AM
  3. Objects disposed incorrectly
    By Osiris43 in forum .NET
    Replies: 1
    Last Post: 08-04-2006, 12:15 PM
  4. JDOM Classpath Help Required
    By kpandya in forum Java
    Replies: 5
    Last Post: 01-15-2006, 07:10 PM
  5. Replies: 5
    Last Post: 10-17-2002, 01:58 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links