Help with Classes and .h files

I have to make a .h file (with classes), link it with a .cpp file and g++ -c to create a .o file. Once I do that I have to link it with another .cpp file to get the results. And we have to do all this classes. It worked for me without classes but now I'm stuck. Right now I'm linking myint.h to myint.cpp to create a .o file (myint.o). Then I'm doing g++ -o main.cpp myint.o.
The errors I keep getting is:
main.cpp: In function 'int main()':
main.cpp:7:19: error: could not convert '5' from 'int' to 'MyInt'
main.cpp:8:23: error: could not convert '20' from 'int' to 'MyInt'
main.cpp:9:30: error: could not convert '32' from 'int' to 'MyInt'
I've tried everything I can think of.
Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//myint.h
#ifndef MYINT_H
#define MYINT_H
#include <iostream>
#include <string>
using namespace std;
class MyInt{
 public:
	int num;
};

MyInt myMax (MyInt x, MyInt y);
MyInt myMin (MyInt x, MyInt y);
MyInt myMiddle (MyInt x, MyInt y, MyInt z);
void prettyPrint (string word, int year);

#endif 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//myint.cpp
#include <iostream>
#include <string>
#include "myint.h" 
using namespace std;

MyInt myMax (MyInt x, MyInt y) { 
	return x.num > y.num ? x : y;
}
MyInt myMin (MyInt x, MyInt y){
	return x.num < y.num ? x : y;
}

MyInt myMiddle (MyInt x, MyInt y, MyInt z){
	if (x.num > y.num && x.num < z.num) {return x;}
	else if (x.num > z.num && x.num < y.num) {return x;}
	else if (y.num > x.num && y.num < z.num) {return y;}
	else if (y.num > z.num && y.num < x.num) {return y;}
	else if (z.num > y.num && z.num < x.num) {return z;}
	else if (z.num > x.num && z.num < y.num) {return z;}
}
void prettyPrint (string word, int year){
	cout << word << ": " << year;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
//main.cpp
#include <iostream>
#include <string>
#include "myint.h"

using namespace std;
int main(){
	cout << myMax(5,9) << endl;
	cout << myMin (20, 18) << endl;
	cout << myMiddle (32, 87, 45) << endl;
	prettyPrint ("year", 2013);
	return 0;
}
I just saw it and while it's informative, I don't think it is what I need because my functions are not part of my class. The functions are separate but incorporate the class MyInt in them. Are you trying to say that my functions should be in the class?
You are not passing `MyInt' objects to your functions, you are passing integers.
1
2
3
4
5
class MyInt{
public:
	MyInt(int num): num(num){}
	int num;
};


Also, `cout' does not know how to print `MyInt' objects.
cout << myMax(5,9).num << endl;

PS: your class shouldn't be a class.
Last edited on
Can you explain what you did here:
1
2
	MyInt(int num): num(num){}
	int num;

It solved it in xcode but I have yet to try it in vim.
That's a constructor.
It tells how can you create `MyInt' objects with an integer.

`myMax(5,9)' would be see as `myMax( MyInt(5), MyInt(9) )'
It may be better to write it directly as that.

> It solved it in xcode but I have yet to try it in vim.
`vim' is not a compiler
Can you explain what you did here:

MyInt(int num): num(num){} int num;

It solved it in xcode but I have yet to try it in vim.


That is: the constructor takes a variable num, and gives num(class member) the value of num(parameter of function)

Aceix.
Thanks that helps.
Topic archived. No new replies allowed.