26813

Struct Inclusion/Implementation Help Needed (c++)

Question:

I'm trying to adapt a bit of code for a doubly-linked list to be included and used in my mysh.cpp file, and I'm getting

error: aggregate ‘linked_list list’ has incomplete type and cannot be defined

on compile. readcommand.cpp compiles just fine, so I'm just trying to figure out what needs to be changed either in the header file or cpp files to get it running smoothly for mysh.

Here are the relevant portions of the files used:

<strong>mysh.cpp</strong>

#include "readcommand.h" using namespace std; int main (int argc, char** argv) { readcommand read; linked_list list; // THIS is the line that's causing the error ... }

<strong>readcommand.h</strong>

#ifndef READCOMMAND_H #define READCOMMAND_H #include <cstdio> #include <iostream> #include <cstring> #include <cstdlib> class readcommand { public: // Struct Definitions typedef struct node node_t; typedef struct linked_list linked_list_t; struct node; struct linked_list; ... }; #endif

<strong>readcommand.cpp</strong>

#include "readcommand.h" using namespace std; struct node { const char *word; node *prev; node *next; }; struct linked_list { node *first; node *last; }; ... <hr />

It's been a while since I've used headers in c++, or the language in general. I've tried changing the line in question to

read.linked_list list;

and

read.linked_list list = new linked_list;

and the like, but it just changes the error to things like

error: ‘class readcommand’ has no member named ‘linked_list’

and

error: invalid use of ‘struct readcommand::linked_list’

Thanks ahead of time.

Answer1:

You need to put these...

struct node { const char *word; node *prev; node *next; }; struct linked_list { node *first; node *last; };

...someplace where the compiler will see them <em>before</em> they are used in class readcommand. Probably the easiest thing to do would be to put them in readcommand.h <em>before</em> class readcommand. The problem is that node and linked_list are being used in your class readcommand but the compiler doesn't know what they are at that point in the compilation.

Answer2:

The struct/class definition (and not just declaration) needs to be visible when you

<ol><li>dereference a pointer to that struct/class</li> <li>create an object of that struct/class</li> </ol>

The compiler needs to know the size of the object and the location of its fields.

That is why you may want to put the definitions of node and linked_list into the .h file. Typically, you put into .cpp only the definitions of member functions.

Answer3:

You have the definition of linked_list in the cpp file... so if you include the .h file, the compiler doesn't see that struct's definition.

Move the struct's definition to the header file.

Answer4:

In readcommend.h

linked_list is a member of class readcommand, you can access it through readcommand object or move linked_list to readcommand.h insead if readcommand.cpp so compiler know "what is it"

Recommend

  • Anyone installed rails 3 on solaris 10x86
  • Android-Ndk vs Cross-Compile? Both work ,What was the need of Android NDK then ?
  • Copying text from one EditText box to another in the same Activity on the fly, character-by-characte
  • Pass Arbitrary Sized 2 Dimension Array
  • What is lua_len() alternative in Lua 5.1?
  • std::system Exception when instantiating a singleton object
  • Call a macro with parameters : Python win32com API
  • Get the App path without the app name at the end of the app
  • PHP + XML - how to rename and delete XML elements using SimpleXML or DOMDocument?
  • Getting short path in python
  • Why must we declare a variable name when adding a method to a struct in Golang?
  • How to read piped content in C?
  • Python ImageIO Gif Set Delay Between Frames
  • C: Incompatible pointer type initializing
  • Textfile Structure (tables)
  • Using $this when not in object context
  • req.body is undefined - nodejs
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • AES padding and writing the ciphertext to a disk file
  • output of program is not same as passed argument
  • Perl system calls when running as another user using sudo
  • Convert array of 8 bytes to signed long in C++
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Akka Routing: Reply's send to router ends up as dead letters
  • Return words with double consecutive letters
  • Linker errors when using intrinsic function via function pointer
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • Buffer size for converting unsigned long to string
  • SQL merge duplicate rows and join values that are different
  • Hits per day in Google Big Query
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to get Windows thread pool to call class member function?
  • LevelDB C iterator
  • unknown Exception android
  • Can't mass-assign protected attributes when import data from csv file
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • java string with new operator and a literal