Class Implementation Conundrum (cpp and h files)

Hello, I have tried to create a new class and implement it as a cpp file and a header. All of my attempts at using the tutorials yield the same problem, I get a load of syntax errors. I've tried deleting the class and using the IDE's create a new class function as well with no avail. My source code runs fine without the classes and doesn't reference the class at all.

Header File:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 #pragma once
class CodexData
{
public:
	string *getKnownWords();
	int *getTopicCodes(); 
	string getData(int code); 
	CodexData(string book); 
	~CodexData(); 
private:
	string *words;
	int *topic_codes; 
	bool setWords(string book);  
	void resize(int size); 
	void error(string message);
	string intToString(int i);
};



Cpp File:

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "CodexData.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

CodexData::CodexData(string book)
{
	if (setWords(book))
	{

	}
	
}

string *getKnownWords()
{

}

int *getTopicCodes()
{

}

string getData(int code)
{

}

void resize(int size)
{

}

void error(string message)
{

}

bool CodexData::setWords(string book)
{
	ifstream input("files/books/" + book, ios::in);
	if (input.fail())
	{
		error("Invalid filename.");
		return false;
	}

	string line;
	int counter(0);
	words = new string[5]; // Start with an initial of 5. Increase as needed.

	while (!input.eof())
	{
		getline(input, line);
		const int equals(line.find('='));

		try
		{
			topic_codes[counter] = stoi(line.substr(0, equals));
			words[counter] = line.substr(equals, line.find('|'));

		}
		catch (invalid_argument)
		{
			error("File corruption: " + book + " file. Line " + intToString(counter));
			return false; 
		}
		
	}

}

string CodexData::intToString(int integer)
{
	string str("");
	int divider(10);
	int length(0);
	char x;
	while (integer / divider >= 1)
	{
		divider *= 10;
		++length;
	}

	for (int i = length; i >= 0; --i)
	{
		x = 48 + floor(integer / pow(10, i));
		integer = integer % static_cast<int>(pow(10, i));
		str += x;
	}
	return str;
}

CodexData::~CodexData()
{
}


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
Error	1	error C2143: syntax error : missing ';' before '*'	c:\users\louis\documents\visual studio 2013\projects\karlaiproject\karlaiproject\codexdata.h	6	1	KarLaiProject
Error	2	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\louis\documents\visual studio 2013\projects\karlaiproject\karlaiproject\codexdata.h	6	1	KarLaiProject
Warning	3	warning C4183: 'getKnownWords': missing return type; assumed to be a member function returning 'int'	c:\users\louis\documents\visual studio 2013\projects\karlaiproject\karlaiproject\codexdata.h	6	1	KarLaiProject
Error	4	error C2146: syntax error : missing ';' before identifier 'getData'	c:\users\louis\documents\visual studio 2013\projects\karlaiproject\karlaiproject\codexdata.h	8	1	KarLaiProject
Error	5	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\louis\documents\visual studio 2013\projects\karlaiproject\karlaiproject\codexdata.h	8	1	KarLaiProject
Warning	6	warning C4183: 'getData': missing return type; assumed to be a member function returning 'int'	c:\users\louis\documents\visual studio 2013\projects\karlaiproject\karlaiproject\codexdata.h	8	1	KarLaiProject
Error	7	error C2061: syntax error : identifier 'string'	c:\users\louis\documents\visual studio 2013\projects\karlaiproject\karlaiproject\codexdata.h	9	1	KarLaiProject
Error	8	error C2143: syntax error : missing ';' before '*'	c:\users\louis\documents\visual studio 2013\projects\karlaiproject\karlaiproject\codexdata.h	12	1	KarLaiProject
Error	9	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\louis\documents\visual studio 2013\projects\karlaiproject\karlaiproject\codexdata.h	12	1	KarLaiProject
Error	10	error C2061: syntax error : identifier 'string'	c:\users\louis\documents\visual studio 2013\projects\karlaiproject\karlaiproject\codexdata.h	14	1	KarLaiProject
Error	11	error C2061: syntax error : identifier 'string'	c:\users\louis\documents\visual studio 2013\projects\karlaiproject\karlaiproject\codexdata.h	16	1	KarLaiProject
Error	12	error C2146: syntax error : missing ';' before identifier 'intToString'	c:\users\louis\documents\visual studio 2013\projects\karlaiproject\karlaiproject\codexdata.h	17	1	KarLaiProject
Error	13	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\louis\documents\visual studio 2013\projects\karlaiproject\karlaiproject\codexdata.h	17	1	KarLaiProject
Warning	14	warning C4183: 'intToString': missing return type; assumed to be a member function returning 'int'	c:\users\louis\documents\visual studio 2013\projects\karlaiproject\karlaiproject\codexdata.h	17	1	KarLaiProject
Error	15	error C2511: 'CodexData::CodexData(std::string)' : overloaded member function not found in 'CodexData'	c:\users\louis\documents\visual studio 2013\projects\karlaiproject\karlaiproject\codexdata.cpp	9	1	KarLaiProject
Error	16	error C2660: 'CodexData::setWords' : function does not take 1 arguments	c:\users\louis\documents\visual studio 2013\projects\karlaiproject\karlaiproject\codexdata.cpp	10	1	KarLaiProject
Error	17	error C2511: 'bool CodexData::setWords(std::string)' : overloaded member function not found in 'CodexData'	c:\users\louis\documents\visual studio 2013\projects\karlaiproject\karlaiproject\codexdata.cpp	43	1	KarLaiProject
Error	18	error C2660: 'CodexData::error' : function does not take 1 arguments	c:\users\louis\documents\visual studio 2013\projects\karlaiproject\karlaiproject\codexdata.cpp	47	1	KarLaiProject
Error	19	error C2065: 'words' : undeclared identifier	c:\users\louis\documents\visual studio 2013\projects\karlaiproject\karlaiproject\codexdata.cpp	53	1	KarLaiProject
Error	20	error C2597: illegal reference to non-static member 'CodexData::topic_codes'	c:\users\louis\documents\visual studio 2013\projects\karlaiproject\karlaiproject\codexdata.cpp	62	1	KarLaiProject
Error	21	error C3867: 'CodexData::topic_codes': function call missing argument list; use '&CodexData::topic_codes' to create a pointer to member	c:\users\louis\documents\visual studio 2013\projects\karlaiproject\karlaiproject\codexdata.cpp	62	1	KarLaiProject
Error	22	error C2109: subscript requires array or pointer type	c:\users\louis\documents\visual studio 2013\projects\karlaiproject\karlaiproject\codexdata.cpp	62	1	KarLaiProject
Error	23	error C2065: 'words' : undeclared identifier	c:\users\louis\documents\visual studio 2013\projects\karlaiproject\karlaiproject\codexdata.cpp	63	1	KarLaiProject
Error	24	error C2352: 'CodexData::intToString' : illegal call of non-static member function	c:\users\louis\documents\visual studio 2013\projects\karlaiproject\karlaiproject\codexdata.cpp	68	1	KarLaiProject
Error	25	error C2660: 'CodexData::error' : function does not take 1 arguments	c:\users\louis\documents\visual studio 2013\projects\karlaiproject\karlaiproject\codexdata.cpp	68	1	KarLaiProject
Error	26	error C2556: 'std::string CodexData::intToString(int)' : overloaded function differs only by return type from 'int CodexData::intToString(int)'	c:\users\louis\documents\visual studio 2013\projects\karlaiproject\karlaiproject\codexdata.cpp	77	1	KarLaiProject
Error	27	error C2371: 'CodexData::intToString' : redefinition; different basic types	c:\users\louis\documents\visual studio 2013\projects\karlaiproject\karlaiproject\codexdata.cpp	77	1	KarLaiProject
Warning	28	warning C4244: '=' : conversion from 'double' to 'char', possible loss of data	c:\users\louis\documents\visual studio 2013\projects\karlaiproject\karlaiproject\codexdata.cpp	90	1	KarLaiProject
Last edited on
closed account (LA48b7Xj)
Just going to point out that you need to write #include <string> in the header file, but don't write using namespace std; in the header file, just use std::string instead of string.
Last edited on
Hello vis594,

Some of the functions in the cpp file are defined as regular functions and not member functions.
I am not sure why you put the function declarations for setWords() resize() and error() in private. They would work better in public or you would need public functions to access the private functions.

Hope that helps,

Andy
Sorry for the late reply, for the private functions, they are just used by the public functions so I didn't think they'd need to be accessed outside the class. Could that be the reason for the errors? Thanks krako, I'll try that and get back to you.
When you write implementation for the member functions in cpp file, this is how you should write the function header

1
2
3
4
returnType className::functionName()
{
    // Whatever code you need here.
}


Looking at your .cpp file, you did not follow that rule.
For example,
1
2
3
4
string *getKnownWords()
{

}


Should be

1
2
3
4
string * CodexData::getKnownWords()
{

}


Same goes for the rest of your member functions.
Last edited on
All of the member functions have been changed to

1
2
3
4
returnType CodexData::function()
{

}


I've included string in the header file and it still doesn't work. Here's the code with people's suggestions.

Header

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

class CodexData
{
public:
	string *getKnownWords(); // Returns the known words list which is parallel to the topic codes array.
	int *getTopicCodes(); // Returns the topic codes array.
	string getData(int code); // Search the file for data on that topic number and return it.
	CodexData(string book); // Open the file specified ready for reading.
	~CodexData(); // Free up the memory used by dynamic arrays.
private:
	string *words;
	int *topic_codes; // Parallel arrays containing the topic code incrementer e.g words[1] = "for" topic_codes[1] = 2; for loops in java might be topic code + 20 for java. 22.
	bool setWords(string book);  // Sets the words and also the topic codes for that word.
	void resize(int size); // Resizes the dynamic arrays.
	void error(string message);
	string intToString(int i);
};


Cpp file

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "CodexData.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

CodexData::CodexData(string book)
{
	if (setWords(book))
	{

	}
	
}

string CodexData::*getKnownWords()
{

}

int CodexData::*getTopicCodes()
{

}

string CodexData::getData(int code)
{

}

void CodexData::resize(int size)
{

}

void CodexData::error(string message)
{

}

bool CodexData::setWords(string book)
{
	ifstream input("files/books/" + book, ios::in);
	if (input.fail())
	{
		error("Invalid filename.");
		return false;
	}

	string line;
	int counter(0);
	words = new string[5]; // Start with an initial of 5. Increase as needed.

	while (!input.eof())
	{
		getline(input, line);
		const int equals(line.find('='));

		try
		{
			topic_codes[counter] = stoi(line.substr(0, equals));
			words[counter] = line.substr(equals, line.find('|'));

		}
		catch (invalid_argument)
		{
			error("File corruption: " + book + " file. Line " + intToString(counter));
			return false; 
		}
		
	}

}

string CodexData::intToString(int integer)
{
	string str("");
	int divider(10);
	int length(0);
	char x;
	while (integer / divider >= 1)
	{
		divider *= 10;
		++length;
	}

	for (int i = length; i >= 0; --i)
	{
		x = 48 + floor(integer / pow(10, i));
		integer = integer % static_cast<int>(pow(10, i));
		str += x;
	}
	return str;
}

CodexData::~CodexData()
{
}
closed account (LA48b7Xj)
In the header file change all string to std::string

In 'bool CodexData::setWords(string book)' the function reaches the end without returning a value, maybe you want to return true at the end.

#include <cmath> at the top of the cpp file.
Last edited on
Thank you, that worked.
Topic archived. No new replies allowed.