Classes in separate files

Hi, I'm having issues trying to put my class neatly into a file. Let me just show you my concrete problem.

I have a class caled Runes defined like this

file: Runes.h
1
2
3
4
5
6
7
8
#pragma once
class Runes{
private:
	//stuff here

public:
	//more stuff here
};


file: Runes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "Runes.h"
#include <iostream>
#include <fstream>

Runes& Runes::operator=(Runes& r1)
{
        //code here 
}

std::ostream& operator<<(std::ostream& stream, Runes& r1)
{
	stream << "Charm: " << r1.GetName() << " | Power: " << r1.GetPower();
	
	return stream;
}

std::istream& operator>>(std::istream& stream, Runes& r1)
{
        //code here 
}


file: main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include "Runes.h"

using namespace std;

int main()
{
	vector<Runes> rune(1);

	ofstream outfs("test.txt");

	for (int i = 0; i < rune.size(); i++)
		cin >> rune[i];

	for (int i = 0; i < rune.size(); i++)
		outfs << rune[i] << endl;

	return 0;
}


All very basic and simple, but I can't get it to compile because I'm getting like gazzilion errors of all kinds. Errors of undeclared identifiers in Runes.h (concerning the 'string' class), and I know that including <string> fixes it, but then the problem would be with redeclaring it in main(), wouldn't it?

Basically my question is Q1:what am I doing wrong, and if I'm doing most of it wrong, and leading to the answer being to long or complicated, then the more appropriate and simple question would follow, Q2: How to make a class fit neatly into a file, and using it in my main?

(the main problem that I seem to be having is with all the includes and using namespace std mumbo-jumbo.

PS: now I'm thinking that it would probably be better to provide the whole source code (I don't know if what I gave here is enough to find any erroneous thinking), if that's the case just say the word, and I'll slap it on.

Thanks for any help.
Last edited on
closed account (1yvXoG1T)
One problem might be that you include "Runes.h" in main but nothing includes "Runes.cpp". But I am kind of curious, why would you write two different files for one class? Why not include your overloaded operator code in "Runes.h"?
Last edited on
Each .cpp is separate, you can't have included string in your main.cpp and have it be in your Runes.cpp file.
Topic archived. No new replies allowed.