Expresion must have class type

Hi,
im creating a simple class, that opens a text file, copies the text to a string and then prints it, i used a default argument, so it will opens pre-created file
but when i use my funcion print, it says "Expresion must have class type"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// text.h
#include <string>
#include <fstream>
#include <iostream>

class Text
{
	std::string text;
	std::ifstream file;
	void open_file(std::string file_name);

public:
	void print();
	Text(std::string file_name = "file.txt");
	~Text();
};



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
// text.cpp
#include "Text.h"

void Text::open_file(std::string file_name)
{
	std::ifstream file(file_name);
	std::string line;
	while (getline(file, line))
		text += line + "\n";
}

void Text::print()
{
	std::cout << "In this file is written this:\n"
		<< text << std::endl;
}

Text::Text(std::string file_name)
{
	open_file(file_name);
}


Text::~Text()
{
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
// source.cpp
#include <iostream>
#include "Text.h"

using namespace std;

int main()
{
	Text test();
	// test.print(); // Expresion must have class type
        Text test2("text.txt");
        test2.print(); // prints normally
	cin.get();
}
Last edited on
void print();
a function

Text test();
also a function


std::string line;
an object

Text test;
also an object
yea... i see it now, thanks!
Topic archived. No new replies allowed.