Итак, я объявил файл .cpp и файл .cpp. .h с содержимым класса и написал main.cpp, чтобы попробовать протестировать свой код, но столкнулся с непредвиденной ошибкой. Во-первых, вот мои файлы main.cpp, stringConcat.h и stringConcat.cpp —
main.cpp:
Код: Выделить всё
#include
#include
#include
#include
#include "stringConcat.h"
using std::cin;
using std::cout;
using std::endl;
using std::initializer_list;
using std::string;
using std::vector;
int main()
{
char input1[101], input2[101];
cin.getline(input1, 101);
cin.getline(input2, 101);
String str1(input1);
String str2(input2);
String result = str1 + str2;
result.display();
return 0;
}
Код: Выделить всё
class String
{
public:
char *a;
int length;
String(char b[], int size=101)
{
a = &b[0];
length = size;
}
String operator+(const String &other);
void display();
};
Код: Выделить всё
#include
#include "stringConcat.h"
using namespace std;
String String::operator+(const String &other)
{
int total = length + other.length;
char temp[total];
int counter = 0;
for(int i = 0; i < length; i++)
{
if(*(a+i) == '\0')
break;
temp[counter++] = *(a+i);
}
temp[counter++] = ' ';
for(int i = 0; i < other.length; i++)
{
if(*(other.a+i) == '\0')
break;
temp[counter++] = *(other.a+i);
}
temp[counter++] = '\0';
return String(temp, counter);
}
void String::display()
{
for(int i = 0; i < length; i++)
{
if(*(a+i) == '\0')
break;
cout
Подробнее здесь: [url]https://stackoverflow.com/questions/79171381/cout-stops-printing-after-one-prompt-inside-method-of-class-cpp-file[/url]
Мобильная версия