using thread library to call class functions

So i'm trying to create a program, that would make a thread of an infinite function that would keep running, while the program keeps doing other stuff. But i'm having problems making the thread. As far as i can understand, its because im somehow trying to copy somekind of a stream, but what exactly this error means and how to fix it, i havent got a clue.

Main:
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
27
28
29
30
  #include "stdafx.h"
#include <iostream>
#include "PS.h"
#include <ctime>
#include <thread>         // std::this_thread::sleep_for
#include <chrono>         // std::chrono::seconds
//#define _CRT_SECURE_NO_WARNINGS
using namespace std;



int _tmain(int argc, _TCHAR* argv[])
{
	    

   // UI

	   PS obj("PS1.txt", 3);
	   std::thread t1(&PS::getDataIndef, obj);
   
//-----------
//(some other code that should continue while the infinite
//function keeps collecting data)
//-----------


    t1.join ();
	return 0;
}


SP class header:
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
27
28
29
30
31
32
33
#pragma once
#include <fstream>
#include <iostream>
#include <vector>


class PS
{
    public:
        //constructors
		PS(char adrese[80], int IntervalInSec);
        virtual ~PS();
        //read data from node to object once
		void readDataFromNode();
        //get dataset from object
		std::vector<std::string> getDataSet();
        //print the existing dataset
		void print();
		//call readDataFromNode indefinitily with an interval of int Interval
		void getDataIndef ();
    protected:
    private:
         //members
		 std::ifstream myfile ;
         std::vector<std::string> dataset;
         char directory[80];
		 int Interval;
}; //this is the 28th line, which throws an error






and SP class:

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "stdafx.h"
#include "PS.h"

#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <thread>         // std::this_thread::sleep_for
#include <chrono>         // std::chrono::seconds

PS::PS(char adrese[80], int IntervalInSec)
{
    Interval=IntervalInSec;
	strncpy (directory, adrese, sizeof(directory));
    //ctor
}
void PS::readDataFromNode()
{
     myfile.open (directory);
    std::string line;
    if (myfile.is_open())
  {
    while ( getline (myfile,line) )
    {
    dataset.push_back(line);
    }
  }
  myfile.close();
}
void PS::getDataIndef ()
{
	    while (0!=1)
    {
    readDataFromNode();
    std::cout.flush();
    std::this_thread::sleep_for (std::chrono::seconds(Interval));
    }
}
void PS::print()
{
    // some code
}
 std::vector<std::string> PS::getDataSet()
{

    return dataset;
}
PS::~PS()
{
    //dtor
}



And the error:

1
2
3
4
5
6
7
8
9
10
11
12
13
ps.h(28): error C2248: 'std::basic_ifstream<_Elem,_Traits>::basic_ifstream' : cannot access private member declared in class 'std::basic_ifstream<_Elem,_Traits>'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\fstream(827) : see declaration of 'std::basic_ifstream<_Elem,_Traits>::basic_ifstream'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          This diagnostic occurred in the compiler generated function 'PS::PS(const PS &)'
It's weird, i tried to
 
 std::thread t1(&PS::getDataIndef, &obj);

change the object that i'm sending to an adress to the object and it compiled. So i guess, ive solved it ! :)
Last edited on
Topic archived. No new replies allowed.