I'm learning c++ and trying to practice translation units and other things at once, but I'm getting the errors listed on the title. This program is for learning purposes and I've tried to explain what each header and implementation file is suppose to do.
--------CString.h--------
#ifndef CSTRING_H
#define CSTRING_H
#include <iostream>
namespace w1
{
class CString
{
public:
char mystring[];
CString(char cstylestring[]);
void display(std::ostream &os);
};
std::ostream &operator<< (std::ostream &os, CString &c)
{
c.display(os);
return os;
}
}
#endif
--------process.h-------- prototype for process function
void process(char cstylestring[]);
--------CString.cpp-------- To receive a C-style string in constructor and truncate it by taking the first three characters and storing it in mystring to be later displayed through the function display()
#include <iostream>
#include "CString.h"
#define NUMBEROFCHARACTERS 3
using namespace w1;
w1::CString::CString(char stylestring[])
{
if (stylestring[0] == '\0')
{
mystring[0] = ' ';
}
else
{
for (int i = 0; i < NUMBEROFCHARACTERS; i++)
{
mystring[i] = stylestring[i];
}
}
//strncpy(mystring, stylestring, NUMBEROFCHARACTERS);
}
void w1::CString::display(std::ostream &os)
{
std::cout << mystring << std::endl;
}
--------process.cpp-------- receives a C-style string and creates a CString object and then display the possibly truncated version of the c-style string by operator overloading.
#include "process.h"
#include "CString.h"
#include <iostream>
using namespace std;
void process(char cstylestring[])
{
w1::CString obj(cstylestring);
std::cout << obj << std::endl;
}
--------main.cpp-------- Testing purposes by sending a C-style string to process function.
#include <iostream>
#include "process.h"
using namespace std;
int main()
{
char themainstring[] = "hiiiii";
process(themainstring);
return 0;
}