Public Group

# File input and output

This topic is 3697 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello I have this huge problem whenever I try to compile this code (i'm using the C++ language): main.cpp
// main.cpp

#include "Wiz.h"
using namespace std;

int main()
{
// Create wizards with specific data.

Wizard wiz0("Gandalf", 25, 100, 10);
Wizard wiz1("Loki", 50, 150, 12);
Wizard wiz2("Magius", 10, 75, 6);

// Create a stream which will transfer the data from
// our program to the specified file "wizdata.tex".

ofstream outFile("wizdata.txt");

// If the file opened correctly then call save methods.

if( outFile )
{
// Dump data into the stream.
wiz0.save(outFile);
wiz1.save(outFile);
wiz2.save(outFile);

outFile.close();
}

}

Wiz.cpp
// Wiz.cpp

#include "Wiz.h"
#include <iostream>
using namespace std;

Wizard::Wizard()
{
mName = "Default";
mHitPoints = 0;
mMagicPoints = 0;
mArmor = 0;
}

Wizard::Wizard(string name, int hp, int mp, int armor)
{
mName = name;
mHitPoints = hp;
mMagicPoints = mp;
mArmor = armor;
}

void Wizard::print()
{
cout << "Name= " << mName << endl;
cout << "HP= " << mHitPoints << endl;
cout << "MP= " << mMagicPoints << endl;
cout << "Armor= " << mArmor << endl;
cout << endl;
}

Wiz.h
// Wiz.h
#ifndef WIZARD_H
#define WIZARD_H
#include <fstream>
#include <string>

class Wizard
{
public:
Wizard();
Wizard(std::string name, int hp, int mp, int armor);

// [...] other methods snipped
void print();
void save(std::ofstream& outFile);
private:
std::string mName;
int mHitPoints;
int mMagicPoints;
int mArmor;
};
#endif // WIZARD_H

And with that compiled I get compile errors as follows:
------ Build started: Project: Saving.Loading, Configuration: Debug Win32 ------

Compiling...
Wiz.cpp
main.cpp
Generating Code...
main.obj : error LNK2019: unresolved external symbol "public: void __thiscall Wizard::save(class std::basic_ofstream<char,struct std::char_traits<char> > &)" (?save@Wizard@@QAEXAAV?$basic_ofstream@DU?$char_traits@D@std@@@std@@@Z) referenced in function _main

---------------------- Done ----------------------

Build: 0 succeeded, 1 failed, 0 skipped



##### Share on other sites
nevermind fixed it

##### Share on other sites
You should post a small explanation on how you solved it and what was wrong, as someone else might have the same problem in the future. (A tip for next time)

##### Share on other sites
The error message says, in effect, "I can't find the Wizard::save" function. This is not surprising, given that it doesn't appear in the Wizard.cpp that you posted. :)

But anyway, the normal way to 'save' and 'load' in C++ is to overload the >> and << operators. That way, you can output the objects with the same syntax you use for primitives, and you can use any stream objects you like. (There is really no reason to restrict yourself to fstreams when you define that functionality.)

Also, there is no need to .close() the file explicitly. It will take care of itself. The .close() member function is provided for special situations.

##### Share on other sites
Quote:
 Original post by ZahlmanBut anyway, the normal way to 'save' and 'load' in C++ is to overload the >> and << operators. That way, you can output the objects with the same syntax you use for primitives, and you can use any stream objects you like. (There is really no reason to restrict yourself to fstreams when you define that functionality.)

I see no wrong with to/from Binary/Network/Text functions.
wizard.toBinary(file) is more to the point than file << wizard;

However its been a long time since I've needed to save structures :)

• 16
• 11
• 9
• 9
• 49
• ### Forum Statistics

• Total Topics
631392
• Total Posts
2999740
×