Help creating class objects

Pages: 12
The code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
using namespace std;
	
class Row 
{
public:
	
	int nLength;

			
	 Row();
	 Row(int length)
	 {
		 this->nLength = length;
	 }

	
};
void main()
{
	Row a(2);
	Row b(2);

	Row rrr[2];
	rrr[0] = a;
	rrr[1] = b;

	cout << rrr[0].nLength << endl;
}


The Errors:

Error 1 error LNK2019: unresolved external symbol "public: __thiscall Row::Row(void)" (??0Row@@QAE@XZ) referenced in function _main Desktop\test_project\test_project\testRow.obj test_project

LNK 1120
1 unresolved external


The errors occur only when I try to create an array of row objects ("rrr").
When I don't - it works perfect.
Thanks!
Last edited on
Error codes. I always wondered if real programmers remember any of those. Anyway, you should post the full compilation errors.

That being said, main() must return an int.
1
2
3
4
5
int main(int argc, char** argv)
{
  // Code
  return 0;
}
Last edited on
you have to first overload the operator [] for row. if you dont know how to do that then do vector<row> rrr(2). then you can treat it like a normal array
Thanks.

maeriden - I just edited my post.

Aramil of Elixia - I know how to overload the operator [] but what should be
in "the body"?
Anyway - I can't understand what do you mean when you say: "do vector<row> rrr(2)" <--- Where?
Please write the full main with the vector <> ..... thanks!
you have to first overload the operator []

No, 'rrr' is a standard array, not a Row object
maeriden - I think you're right.

I'm coming from C# world and it's so so so easy!

What's the problem creating an array of objects of class THAT I HAVE BEEN CREATED?!

;)
yes its a row object. he declared it with type row. so you have to overload the [] operator which i dont know how to do and @ blabla just #include <vector> then @ line 24 replace whats there with the vector code
This is what I get now:

"Error 1 error LNK2019: unresolved external symbol "public: __thiscall Row::Row(void)" (??0Row@@QAE@XZ) referenced in function "void __cdecl std::_Uninit_def_fill_n<class Row *,unsigned int,class Row,class std::allocator<class Row>,class Row>(class Row *,unsigned int,class Row const *,class std::allocator<class Row> &,class Row *,struct std::_Nonscalar_ptr_iterator_tag)" (??$_Uninit_def_fill_n@PAVRow@@IV1@V?$allocator@VRow@@@std@@V1@@std@@YAXPAVRow@@IPBV1@AAV?$allocator@VRow@@@0@0U_Nonscalar_ptr_iterator_tag@0@@Z) Desktop\test_project\test_project\testRow.obj test_project"

plus

LNK 1120
post your code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <vector>
using namespace std;
	
class Row 
{
public:
	
	int nLength;


	Row();
	 Row(int length)
	 {
		 this->nLength = length;
	 }

	
};
void main()
{
	Row a(2);
	Row b(2);

	vector<Row> rrr(2);
	
	rrr[0] = a;
	rrr[1] = b;

	cout << rrr[0].nLength << endl;

}
line 12 your constructor needs to have {} and not the ;. i could be wrong but i think you have to overload the = operator before doing lines 28 and 27 do instead do the data members
yes its a row object.

No, it's an array of 2 Row objects

Quoting a guy from stackoverflow about LNK2019
This error often means that some function has a declaration, but not a definition.

You have to define Row's default ctor, even if it doesn't do anything
now your getting nit picky. i wrote that because you said rrr is a standard array not a row object. and actually no. it is a row object that holds two rows. like int array[2] is an int array that holds 2 ints. its type is still int object
OMG it works!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using namespace std;
	
class Row 
{
public:
	
	int nLength;


	Row()
	{

	}
	 Row(int length)
	 {
		 this->nLength = length;
	 }

	
};
void main()
{
	Row a(2);
	Row b(2);

	Row* rrr = new Row[2];
	
	rrr[0] = a;
	rrr[1] = b;

	cout << rrr[0].nLength << endl;

}


The stupi****......


Thanks again.
ur welcome.... wait it let you do an array? thats wierd...
I suggest modifying main to make it like a standard main. I remember people having errors because of that

its type is still int object

Its type is int*
yup
like int array[2] is an int array that holds 2 ints. its type is still int object


No it isn't. It's type is an array of int objects. If you try and use array as if it were an int, you will run into all sorts of problems.

C++ is a typed language, and you need to be precise about types when talking about it.

In your example:

array[0] is an int object.
array[1] is an int object.

array is not an int object. If you treat it as an int object, then you will make mistakes.

You can call it "nit picky", but successful programming is about being nit picky.
Last edited on
Hi MikeyBoy -

Can you extend about nicpicky?

but there are diffrent things to be nit picky about. yes i now realize my mistake they r *'s, but how is he using the [] operator
Pages: 12