Why do I get errors when I try to modularize my code?

It runs fine when everything is in one file, but when I put the class, header, and driver in different files, I get errors. 11 errors to be exact. The first code is without modularization. The three after it is with modularization. I comment the error in the 3 after.

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <string>

using namespace std;

class Array
{
private:
	int *ptr;
	int size;
public:
	Array();
	~Array();
	Array(int*, int);
	int& operator[] (string s);
	int& operator[] (int index);
	void print();
};

Array::Array()
{

}


Array::~Array()
{

}

int& Array::operator[] (string s)
{
	int index;
	index = s[0] - 65; // or 13 instead of 65

	while (ptr[index] != 0)
	{
		index = (index + 1) % 6;
	}

	return ptr[index];
}

int& Array::operator[] (int index)
{
	return ptr[index];
}

Array::Array(int* p = nullptr, int s = 0)
{
	size = s;
	ptr = nullptr;

	if (s != 0)
	{
		ptr = new int[s];
		for (int i = 0; i < s; i++)
		{
			ptr[i] = p[i];
		}
	}
}

void Array::print()
{
	for (int i = 0; i < size; i++)
	{
		cout << ptr[i] << " ";
	}

	cout << endl;
}

int main()
{
	int a[] = { 0, 0, 0, 0, 0, 0 };
	Array arr1(a, 6);
	arr1["Apple"] = 99;
	arr1[1] = 100;
	arr1["Ape"] = 50;
	arr1.print();

	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//HEADER
#pragma once
#include <iostream>
#include <string>

class Array
{
private:
	int *ptr;
	int size;
public:
	Array();
	~Array();
	Array(int*, int);
	int& operator[] (string s); /* 1) syntax error: identifier string 2) binary operator has too few parameters*/
	int& operator[] (int index);
	void print();
};


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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//IMPLEMENTATION
#include <iostream>
#include <string>
#include "Array.h"

using namespace std;

Array::Array()
{

}


Array::~Array()
{

}

int& Array::operator[] (string s) /* 5) 'int &Array::operator [](std::string)': overloaded member function not found in 'Array'*/	
{
	int index;
	index = s[0] - 65; // or 13 instead of 65

	while (ptr[index] != 0) /* 6) illegal reference to non-static member 'Array::ptr 7) 'Array::ptr': non-standard syntax; use '&' to create a pointer to member 8) subscript requires array or pointer type*/
	{
		index = (index + 1) % 6;
	}

	return ptr[index]; /* 9) illegal reference to non-static member 'Array::ptr' 10) 'Array::ptr': non-standard syntax; use '&' to create a pointer to member 11) subscript requires array or pointer type*/
}

int& Array::operator[] (int index)
{
 	return ptr[index];
}

Array::Array(int* p = nullptr, int s = 0)
{
	size = s;
	ptr = nullptr;

	if (s != 0)
	{
		ptr = new int[s];
		for (int i = 0; i < s; i++)
		{
			ptr[i] = p[i];
		}
	}
}

void Array::print()
{
	for (int i = 0; i < size; i++)
	{
		cout << ptr[i] << " ";
	}

	cout << endl;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//DRIVER
#include <iostream>
#include <string>
#include "Array.h"

using namespace std;

int main()
{
	int a[] = { 0, 0, 0, 0, 0, 0 };
	Array arr1(a, 6);
	arr1["Apple"] = 99; /* 3)  binary '[': no operator found which takes a right-hand operand of type 'const char [6]' (or there is no acceptable conversion)*/
	arr1[1] = 100;
	arr1["Ape"] = 50; /* 4) binary '[': no operator found which takes a right-hand operand of type 'const char [4]' (or there is no acceptable conversion)*/	
	arr1.print();

	return 0;
}
The difference is using namespace std;. In your header it is actually missing.

I suggest the you write std::string. See:

https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
When you get multiple errors it's always best to start with the first error in the list because later errors can be side effects of the first error. Fixing the first error might fix many, if not all, of the other errors.

When I compile Array.h the first error I get is:
1
2
3
Array.h:15:19: error: declaration of ‘operator[]’ as non-function
  int& operator[] (string s);
                   ^~~~~~

This is a confusing error message to be honest, but the problem seems be that you have not put std:: in front of string.

 
	int& operator[] (std::string s); // OK 

After fixing this error the other files seem to compile just fine.
Last edited on
Topic archived. No new replies allowed.