943,101 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 762
  • C++ RSS
Feb 8th, 2010
0

Using C library with anonymous struct in C++

Expand Post »
Hello--

I've been writing some code in C++ to test the tffs-lib library, which is written in C. The library provides functions for directly accessing a FAT file system from within a program. Although most modern operating systems have drivers for accessing the FAT filesystem on SD cards, I am working with an embedded Linux system, and I would like to directly access the FAT filesystem using a device node (/dev/mmcblk1p1) to speed up write I/O access.

I am able to build the tffs-lib and statically link it with my test program. My cross-compiler build is based on gcc 4.2.2.

However, I receive the following error when compiling the code. This error mentions an "anonymous struct," which I believe is not legal in C++.

C++ Syntax (Toggle Plain Text)
  1. request for member 'fatsz' in 'htffs', which is of non-class type '<anonymous struct>*' test.cpp test-tffs-lib

Is it possible to use an anonymous struct in C++, even when the anonymous struct is being used within a C library? I tried passing the command-line switch "-features=extensions" to gcc, but (strangely enough) this results in no executable being created. However, after passing this switch, there is no warning generated, and the code compiles cleanly (without the output executable being created.)

Here is the code of my very simple test program in C++. The error occurs on line 32 below.

C++ Syntax (Toggle Plain Text)
  1.  
  2. #include <iostream>
  3. #include <string>
  4.  
  5. // TFFS library include (for C code)
  6. extern "C" {
  7. #include <tffs.h>
  8. }
  9.  
  10. void doTest();
  11.  
  12. int main()
  13. {
  14. doTest();
  15. return 0;
  16. }
  17.  
  18. void doTest()
  19. {
  20. int32_t ret;
  21. std::string sd_dev = "/dev/mmcblk1p1";
  22. tffs_handle_t htffs;
  23.  
  24. // mount the filesystem directory
  25. ret = TFFS_mount((byte*)sd_dev.c_str(), &htffs);
  26. if(ret != TFFS_OK)
  27. exit(1);
  28.  
  29. // check the space available on the medium
  30.  
  31. // This is the line of code where the error occurs
  32. uint32_t fatsz = htffs.fatsz;
  33. std::cout << "fatsz = " << fatsz << std::endl;
  34.  
  35. // unmount the filesystem directory
  36. ret = TFFS_umount(htffs);
  37. if(ret != TFFS_OK)
  38. exit(1);
  39. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
nkinar is offline Offline
16 posts
since Jan 2010
Feb 8th, 2010
1
Re: Using C library with anonymous struct in C++
> uint32_t fatsz = htffs.fatsz;
Well that's the whole POINT of handles, and that is to be opaque for external users.

It's basically the same as the 'this' pointer if you had a C++ class. It's just a magic token you pass to each "member" function of the public interface so it can distinguish between various instances.

It is no more wise to try and smash your way through an anonymous struct to get at the data behind it, than it is to smash your way through a class to rummage around with the private variables.
Team Colleague
Reputation Points: 5862
Solved Threads: 949
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Feb 8th, 2010
0
Re: Using C library with anonymous struct in C++
Ah, okay - thanks for pointing that out, Salem! I will have to obtain the size of the FAT disk by some other way.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
nkinar is offline Offline
16 posts
since Jan 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Duplication
Next Thread in C++ Forum Timeline: Car Class Homework HELP!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC