H Files problem.

Fairly new to C++ programming. Experimenting with multiple files a class I've created won't run. No errors, just won't run. Code below:

1
2
3
4
5
6
7
8
9
10
11
12
//in main.cpp
#include "MyClass.h"  // defines MyClass
#include <iostream>
using namespace std;

int main()
{
	cout << "Output Numbers";
	MyClass foo;
	cin.get();
	return 0;
}


1
2
3
4
5
6
7
//MyClass.h
class MyClass
{
	public:
		
		int foo();
};


1
2
3
4
5
6
7
8
9
10
11
#include "myclass.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int MyClass::foo()
{
	printf ("Number: %d\n", rand() % 100);
	srand ( time(NULL) );
	return 0;
}


Last edited on
What do you mean by "won't run"?
It's working fine. You just never call the function you made. Try this instead in main:

1
2
3
4
5
6
7
8
9
10
11
12
13
//in main.cpp
#include "MyClass.h"  // defines MyClass
#include <iostream>
using namespace std;

int main()
{
	cout << "Output Numbers";
	MyClass foo;
	foo.foo(); //Call the function foo() which is a member of MyClass
	cin.get();
	return 0;
}


Although MyClass.foo() will always output the same random number unless you call it more than once, or call srand() before outputting the random number.
Last edited on
Thanks a lot, works perfectly now. :)
Topic archived. No new replies allowed.