class, object, parameter

Please correct this and in comments there are two questions, answer please...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #define test testcase_001;             //parameter    
  #include "testcases/test.cpp"           //could i pass parametrised value here?

  class hci_test
 {  
      public:
      unsigned char* host_hci_pkt_arr;
      unsigned int address;           
      test test_case;                        //could i make object handle this way?
       hci_test()              //constructor
      {  
         host_hci_pkt_arr = new(nothrow) unsigned char [20];
         host_hci_pkt_arr[0]  = test_case.host_hci_pkt_arr[0]; 	                            
         host_hci_pkt_arr[1]  = test_case.host_hci_pkt_arr[1];
      }
  };   	                            


Regards
cam
Last edited on
what do you mean by
Please correct this
? does the compiler produce error ?

by the way what is test_case_001 ?

Edit

Oh, i'm sorry, i've just seen your question:
could i make object handle this way?

if what you mean is "if you can declare objects that way" well, the answer is YES ( but it is not advisable )
Last edited on
yes there are errors, actually i want implementation or functionality like above code, but don't how exactly it would be implemented in C++?

e.g.

#define a 20;

testcase_001 is like 20, some parameter

Last edited on
e.g.

#define a 20;

testcase_001 is like 20, some parameter


actually 20 is not a parameter, it is the value you want to be aliased by 'a'

http://www.cplusplus.com/doc/tutorial/constants/
Thanks shadow fiend,

but its not working, compiler throwing errors

:2nd line: error: testcases/test.cpp: No such file or directory

:9th line: error: ISO C++ forbids declaration of ‘testcase_001’ with no type

:9th line: error: ISO C++ forbids declaration of ‘test_case’ with no type
:2nd line: error: testcases/test.cpp: No such file or directory


this means that the compiler cannot find the test.cpp file, make sure the path is correct


:9th line: error: ISO C++ forbids declaration of ‘testcase_001’ with no type


that is because the compiler cannot find testcase_001( possibly because of the first error )

:9th line: error: ISO C++ forbids declaration of ‘test_case’ with no type


same as above

~~~~~~~~

can you post your whole code ?

~~

if you get a little confused about multiple files, you can read this article :

http://www.learncpp.com/cpp-tutorial/18-programs-with-multiple-files/
Last edited on

Its working well if instead of test I write testcase_001, problem is in passing parameter. It means we cannot pass parameters as in above code??

//#define test testcase_001; //parameter
#include "testcases/testcase_001.cpp" //could i pass parametrised value here?

class hci_test
{
public:
unsigned char* host_hci_pkt_arr;
unsigned int address;
testcase_001 test_case; //could i make object handle this way?
hci_test() //constructor
{
host_hci_pkt_arr = new(nothrow) unsigned char [20];
host_hci_pkt_arr[0] = test_case.host_hci_pkt_arr[0];
host_hci_pkt_arr[1] = test_case.host_hci_pkt_arr[1];
}
};
Last edited on
can you post the whole code including
testcase_001.cpp

e.g.

#define a 20;

testcase_001 is like 20, some parameter


actually 20 is not a parameter, it is the value you want to be aliased by 'a'

http://www.cplusplus.com/doc/tutorial/constants/

ok.

Means there would be another way to pass parameter. I will check.

But through #define also, it should work?



Last edited on
testcase_001.cpp file:

class testcase_001
{
public:
unsigned char* host_hci_pkt_arr;

unsigned int address;

testcase_001() //constructor
{ /*----------comman/data packet configuration(from host to hci)--------*/
host_hci_pkt_arr = new(nothrow) unsigned char [20];
host_hci_pkt_arr[0] = 0x01;
host_hci_pkt_arr[1] = 0x03;

}
};
Last edited on
testcase_001.cpp :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class testcase_001
{ 
public:
    unsigned char* host_hci_pkt_arr;
    unsigned int address; 

    testcase_001()
    {
        host_hci_pkt_arr = new(nothrow) unsigned char [20];
        
        host_hci_pkt_arr[0] = 0x01; 
        host_hci_pkt_arr[1] = 0x03; 
    }
};


<yourfile>.cpp :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#define test testcase_001   
#include "testcases/testcase_001.cpp"

class hci_test
{
public:
    unsigned char* host_hci_pkt_arr;
    unsigned int address;           
    test test_case;
    
    hci_test()
    {  
        host_hci_pkt_arr = new(nothrow) unsigned char [20];
        
        host_hci_pkt_arr[0]  = test_case.host_hci_pkt_arr[0]; 	                            
        host_hci_pkt_arr[1]  = test_case.host_hci_pkt_arr[1];
    }
};
Last edited on
You should not #include source files.
Given that your *.cpp contains a class definition, it may be more adequate as an *.h, that can be included. It would need header guards

You have #define test testcase_001; //parameter
Macros are simply text substitution, so you'll have
1
2
3
4
5
6
class hci_test
{
public:
    unsigned char* host_hci_pkt_arr;
    unsigned int address;           
    testcase_001; test_case;//note the semicolon  

I don't know where did you get that definition, but that is not what parameters are
http://www.cplusplus.com/doc/tutorial/functions/


> if what you mean is "if you can declare objects that way" well, the answer is YES ( but it is not advisable )
¿why not?
¿what would be the "proper" way?
ne555 wrote:
¿why not?
¿what would be the "proper" way?


what i mean is #defines are less advisable compared to other alternative ( like typedef or const )
but in the above case it doesn't really matter since he just define an alias for a type
ne555,

testcase_001; test_case;//note the semicolon

why is there semicolon in between the statement?
you defined #define test testcase_001; // <--- there is a semicolon here

what happens above is for every occurance of the word test, the compiler replaces it w/ testcase_001; which in your code becomes :
1
2
     test; test_case;
//      ^^^ note that the semicolon typo is also added here producing a compiler error 
k, semicolon here was added by mistake, actually its no there in code, its like below

#define test testcase_001

and i used .h in place of .cpp

But still its not working.

Actually I think there would be another way to pass the parameters from top?
I don't really understand what you are trying to do. Could you give a more detailed explanation about what your goal is here?
I want to pass parameter like i had passed tescase_001 in first line of code likewise this i want to pass testcase_002, 003, 004, and then I want that corresponding file automatically included as done in 2nd line and respective object also created. But I think following is not right way as compiler showing error: cannot find test.cpp.
And I think its all because of first line, may be its not the right way to pass parameter in C++, Actually I am developing some block in systemC. but systemC is inheriting all features, syntax of C++. Thats why I am asking this question here

#define test testcase_001; //parameter
#include "testcases/test.cpp" //could i pass parametrised value here?

class hci_test
{
public:
unsigned char* host_hci_pkt_arr;
unsigned int address;
test test_case; //object
hci_test() //constructor
{
host_hci_pkt_arr = new(nothrow) unsigned char [20];
host_hci_pkt_arr[0] = test_case.host_hci_pkt_arr[0];
host_hci_pkt_arr[1] = test_case.host_hci_pkt_arr[1];
}
};


Regards
cam
Last edited on
Wait, are you saying you want to do something like
1
2
#define test testcase_001
#include "testcases/test.cpp" 

and have the test.cpp become testcase_001.cpp by macro substitution?

I don't think it's possible at all, but maybe someone with a more thorough understanding of the preprocessor can step in....

The best I have to offer is
1
2
3
4
5
6
#ifdef testcase_001
#include "testcases/testcase_001.cpp" // By the way, why are you #including .cpp files?
#elif defined testcase_002
#include "testcases/testcase_002.cpp"
// ... so on and so forth
#endif 
Topic archived. No new replies allowed.