why "error - not declared in this scope"

Hello,

I'm new to C++ and found a snippet of code online that I was trying to modify for my own use. The code initially was designed to prompt the user for a UNIX command and then display the results from of that command.

I needed something where I could save the results in an array/vector, filter through them, and display them as necessary. Below is the modified code, could someone help me get it to compile correctly?

It keeps saying:

ExecCommand.cpp:69: error: 'command' was not declared in this scope


Thank you!

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream> 
#include <string> 
#include <vector> 
#include <algorithm> 
#include <stdexcept> 

#include <cstdio> 
#include <cstdlib> 
 
 
class ExecCommand 
{ 
  public: 
    ExecCommand( const std::string &cmd ) 
    { 
      FILE *pfd = popen( cmd.c_str(), "r" ); 

      if ( pfd == 0 ) 
      { 
        throw std::runtime_error( "Command or process could not be executed." ); 
      } 

      while ( !feof(pfd) ) 
      { 
        char buf[ 1024 ] = {0}; 

        if ( fgets(buf, sizeof(buf), pfd) > 0 ) 
        { 
          m_results.push_back( std::string(buf) ); 
        } 
      } 
      pclose( pfd ); 
    } 

    const std::vector<std::string>& getResults() const 
    { 
      return m_results; 
    } 

    friend std::ostream & operator<<( std::ostream &os, const ExecCommand &exec ) 
    { 
      std::for_each( exec.m_results.begin(), exec.m_results.end(), ExecCommand::Displayer(os) ); 
      return os; 
    } 

  private: 
    class Displayer 
    { 
      public: 
        Displayer( std::ostream &os ) : m_os(os) {} 

        void operator()( const std::string &str ) 
        { 
          m_os << str; 
        } 

      private: 
        std::ostream &m_os; 
    }; 
    std::vector<std::string> m_results; 
}; 


void displayString( const std::string &str ) 
{ 
  std::cout << str; 
} 

const vector command(string cmd) {
  try 
  { 
    //std::string cmd; 

    for ( int i = 1; i < argc; ++i ) 
    { 
      cmd += argv[i]; 
      cmd += " "; 
    } 
 
    // execute the cmd and store results 
    // 
    ExecCommand exec( cmd ); 

    // display the results 
    // 
	//std::cout << exec << std::endl; 

    // alternate way to get the results 
     
    //const std::vector<std::string> &results = exec.getResults(); 
    //std::for_each( results.begin(), results.end(), displayString ); 
	
	return results;
	
  } 
  catch ( std::exception &ex ) 
  { 
    std::cerr << "Error: " << ex.what() << std::endl; 
  } 
}

int main( int argc, char **argv ) 
{
	printf("%s", command("users"));
	return 0; 
}  

const vector command(string cmd) should be:
const std::vector<std::string> command(std::string cmd)

Put the commented code back in.
Topic archived. No new replies allowed.