-2

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;
}

1 Answers1

0

The << operator is defined multiple time because you define it in an header file that is include from multiple source files.

Either add inline modifier so that only one copy will be kept or move the definition in one source file (and only keep the declaration in the header file).

As I have mentioned in my comment, the program would crash at run time because no memory is allocated for mystring. If it should be 4 characters long maximum, then you could simply add 4 inside the square bracket as in:

char mystring[4];

Otherwise, if you need variable size, then using something like std::vector might make sense as you would avoid explicit memory management.

Update:

My original answer was complete but since you don't seem to properly understand it, I have added extra details...

I am talking of the following definition in CString.h:

std::ostream &operator<< (std::ostream &os, CString &c)
{
    c.display(os);
    return os;
}

Both process.cpp and main.cpp include the file CString.h which contains that definition twice (once when compiling each of these 2 files).

Phil1970
  • 2,605
  • 2
  • 14
  • 15