Dlaczego pole string wyrzuca błąd podczas cout?

0

Witam. Napotkałem problem z klasami mianowicie poniższy kod wyrzuca błąd:

bl-dy.png

Poniżej kod programu:

#include <iostream>
#include <cstdlib>

#include <fstream>
using namespace std;

struct header {
	char magicNumb[2];
	int width;
	int height;
	int grayScale;

};

class image
{
public:
	header _header;
	string fileName;  
	int **img;

	void set_header(std::string MN, int width, int height, int GS, string Fname)
	{
		this->fileName = Fname;
		this->_header.magicNumb[0] = MN[0];
		this->_header.magicNumb[1] = MN[1];
		this->_header.width = width;
		this->_header.height = height;
		this->_header.grayScale = GS;
	};
	void create_table_of_img(int valWidth, int valHeight)
	{
		this->img = new int*[valWidth];
		for (int i = 0; i < valHeight; ++i) {
			this->img[i] = new int[valHeight];
		}
	}
	void delete_table()
	{
		for (int i = 0; i < this->_header.width; ++i) {
			delete[] this->img[i];
		}
		delete[] img;
	}
	void print_table()
	{
		for (int x = 0; x < this->_header.width; x++)
		{
			for (int y = 0; y < this->_header.height; y++)
			{
				std::cout << img[x][y]<<" ";
			}
			std::cout << "\n";
		}
	}
	void print_header()
	{
		cout << "File name : " << fileName;

		printf("File name: %s", this->fileName);
		printf("magicNumb: %c%c", this->_header.magicNumb[0], this->_header.magicNumb[1]);
		printf("\nWidth: %i", this->_header.width);
		printf("\nheight: %i", this->_header.height);
		printf("\ngrayScale: %i", this->_header.grayScale);
	};
	void read_file()
	{
		fstream file;
		file.open(this->fileName, ios::out | ios::app | ios::binary);


	}


};
0

Brakuje reszty kodu programu. Przygotuj MCVE: https://dsp.krzaq.cc/post/445/jak-zadawac-pytania-na-forum/

0

Proszę:

//------------------------------------------------header.h--------------------------------------------------------------

#include <iostream>
#include <cstdlib>

class image
{
public:
	
	std::string fileName;
	
	void set_header(std::string Fname)
	{
		this->fileName = Fname;
	};
	
	void print_header()
	{
		std::cout << "File name : " << fileName;
	};
};

-------------------------------------------------source.cpp--------------------------------------------------------------

#include <iostream>
#include "header.h"

using namespace std;

int main(int argc, char *argv[])
{
	image test;
	test.set_header("tenplik");
	test.print_header();

	system("pause");
	return(0);
}
0

W takim razie ja nie wiem co ten Vstudio miesza ...
Dziękuje @KrzaQ

3

Możliwe, że implementacja <iostream> w gcc/clang includuje <string>. A tutaj:

std::string fileName;

i tutaj:

std::cout << "File name : " << fileName;

może być potrzebne ciało klasy std::basic_string a nie tylko forward declaration, a np. implementacja <iostream> w Visual Studio robi właśnie tylko forward declaration. Także żeby mieć pewność, że masz definicję std::string to dodaj #include <string>

1

Jaj jeszcze dodam, że zamiast robić screenshota, kopiuj tekst logów budowania (tam widać wszystkie ważne informacje).
W VS powinieneś mieć zakładkę "Output".

0

To jest znany „problem”. Używasz std::string więc powinieneś mieć #include <string>.

1 użytkowników online, w tym zalogowanych: 0, gości: 1