Passing arguments to program, compiler errors in c++ structured program in c it is working

Hi there,

i work on a linux platform and was able to handle arguments in standard c programs in the past.

I am actually a bit frustrated about getting passing arguments to a c++ structured program to run, and I need to say I am not an expert in c++ right now.

I did already consulate my c++ books, but they pay no attention to arguments then I goggled to several websites including this forum, there where a lot hints but I was not able to point out my pitfall...
http://www.cplusplus.com/forum/beginner/26251/
http://www.crasseux.com/books/ctutorial/argc-and-argv.html

I think I have trouble with the right prototypes on the right place.
I actually found no way to get it to compile without errors... :-(
Only when I remark the argument lines and use (void) instead of (int argc, char *argv[])it compiles fine.
I think I need some help here now...

The Program structure is the reduced one of the program, I use in my project, where I need to implement the arguments.

I have on all places remarked lines in the code to be able to exchange them like:
int cApp::main(void)
//int cApp::main(int argc, char *argv[])

When I use the (void) lines the code compiles fine with:
g++ arg_test.cpp -o arg_test

When I use the (int argc, char *argv[]) lines instead of (void) I have different error messages. below I did several permutations of lines commented in and out with between the recompiling, but no success..
Only the following errors

linux:/home/be/connect # g++ arg_test.cpp -o arg_test
arg_test.cpp:33: error: prototype for `int cApp::main(int, char**)' does not
match any in class `cApp'
arg_test.cpp:8: error: candidate is: int cApp::main()
linux:/home/be/connect # g++ arg_test.cpp -o arg_test
arg_test.cpp: In function `int main(int, char**)':
arg_test.cpp:45: error: no matching function for call to `cApp::main()'
arg_test.cpp:33: error: candidates are: int cApp::main(int, char**)
linux:/home/be/connect # g++ arg_test.cpp -o arg_test
arg_test.cpp:25: error: prototype for `cApp::cApp(int, char**)' does not match
any in class `cApp'
arg_test.cpp:3: error: candidates are: cApp::cApp(const cApp&)
arg_test.cpp:10: error: cApp::cApp()
arg_test.cpp: In function `int main(int, char**)':
arg_test.cpp:45: error: no matching function for call to `cApp::main()'
arg_test.cpp:33: error: candidates are: int cApp::main(int, char**)

may be someone of you can point out my pitfall?

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
// Class of application
class cApp
{
public:
	int Test;      // just a vriable

public:
int main(void);  									
//int main(int argc, char *argv[]);  		// error
    cApp(void);  										
//    cApp(int argc, char *argv[]); 		//error								
};

#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
using namespace std;

class cApp App;

// Constructor of applications
cApp::cApp(void)
//cApp::cApp(int argc, char *argv[])		// error
{
	  Test=1;				// just a vriable
};


// Main Programm of Application
int cApp::main(void)
//int cApp::main(int argc, char *argv[])        // error
{
	printf("Hello World :%d\n",Test);
	return 0;
}


// Start of application
int main(void)
//int main(int argc, char *argv[])  		// error
{
  printf("Launch application\n");
  App.main();
  printf("goodbye\n");
  return 0;
}
Last edited on
My guess would be, from the text of the errors that occurred, that when you uncomment line 33, you don't also uncomment line 9. The declaration of a function member in the class definition must match the function member's definition. There really isn't any reason for the commenting, though. Each version can coexist with the other. This isn't C. C++ allows one to overload functions.

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
#include <iostream>

class cApp
{
private:
    int _argc ;
    char** _argv ;


public:

    cApp() ;
    cApp(int argc, char* argv[]) ;

    void printArgs()
    {
        if ( _argc == 0 )
            std::cout << "No args!\n" ;

        for ( int i=0; i<_argc; ++i )
            std::cout << _argv[i] << '\n' ;
    }
};


cApp::cApp() : _argc(0), _argv(nullptr) 
{
}

cApp::cApp(int argc, char* argv[]) : _argc(argc), _argv(argv)
{
}

int main(int argc, char *argv[]) 
{
    cApp app1 ;
    cApp app2(argc, argv) ;

    app1.printArgs() ;
    app2.printArgs() ;
}
Last edited on
Hi cire, Thank you for the hints.
At first I had a problem to compile your code because of the target system I work on is a embedded linux 2.4 with g++ version 3.3.1. There is a problem that nullptr is not supported in older g++ versions. I have solved with hints on that page:
http://stackoverflow.com/questions/3756473/what-header-file-needs-to-be-included-for-using-nullptr-in-g

may be someone else run into same problem when trying it:

So actually my testcode is running. But I hope to adapt it in the right way to my given structure, may be some can crosscheck the way I did it?

To make nullptr work in old g++ versions:
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
 // To be able to use nullptr
namespace std
{
    struct nullptr_t
    {
        template<typename any> operator any * () const
    {
        return 0;
    }
    template<class any, typename T> operator T any:: * () const
    {
        return 0;
    }

#ifdef _MSC_VER
    struct pad {};
    pad __[sizeof(void*)/sizeof(pad)];
#else
    char __[sizeof(void*)];
#endif
private:
    void operator &() const;
    template<typename any> void operator +(any) const
    {
    }
    template<typename any> void operator -(any) const
    {
    }
    };
static const nullptr_t __nullptr = {};
}

#ifndef nullptr
#define nullptr std::__nullptr
#endif 



Actual testcode:
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
class cApp
{
private:
    int _argc ;
    char** _argv ;

public:
int Test; // just a vriable

int main(int argc, char* argv[]);  									
    cApp(int argc, char* argv[]);  										
};

#include <algorithm>
using namespace std;


cApp::cApp(int argc, char* argv[]) : _argc(argc), _argv(argv)
{
   Test=1;				// just a vriable
};


// Main Programm of Application
int cApp::main(int argc, char* argv[])
{
	printf("Hello World\nTestvariable:%d CMD_Arguments:%d\n",Test,argc);
	return 0;
}

// Start of application
int main(int argc, char* argv[])
{
  cApp App(argc, argv);
  
  printf("Launch application\n");
  App.main(argc, argv);
  printf("goodbye\n");
  return 0;
}



Last edited on
Topic archived. No new replies allowed.