0

Hi I am doing an assignment in class to get familiar with using namespaces. I am getting a few different errors although i'm not sure why, as i've done this same process in 4 other header and .cpp files w/o errors so i'm stumped. The errors are:

WriteFile.cpp: In function 'void WriteLine(CSC2100::WriteFile*, CSC2100::String*)':
WriteFile.cpp:39:27: error: invalid use of incomplete type 'struct CSC2100::String'
In file included from WriteFile.h:4:0,
                 from WriteFile.cpp:1;
String.h:6:11: error: forward declaration of 'struct CSC2100::String'
WriteFile.cpp:41:30: error: invalid use of incomplete type 'struct CSC2100::String'
In file included from WriteFile.h:4:0,
                 from Writefile.cpp:1:
String.h:6:11: error: forward declaration of 'struct CSC 2100::String'

I've thoroughly examined the code mentioned in the error and can't seem to find the problem. I've included the forward declaration at the top of the WriteFile.cpp as well as the function that the errors are in:

#include "WriteFile.h"
namespace CSC2100
{
   struct WriteFile
   {
      ofstream output_file;
      bool closed;
   };
}
#include <sstream>
using namespace CSC2100;
///////////////////////////////////////////
void writeLine(WriteFile* wf, String* line)
{
   if (!wf->closed && line->sz > 0)
   {
      wf->output_file << line->text << endl;
   }
}

here is WriteFile.h :

#include "String.h"

#include <fstream>
using namespace std;

namespace CSC2100
{
   struct WriteFile;
}
CSC2100::WriteFile* createWriteFile(const char* file_name);
void destroyWriteFile(CSC2100::WriteFile* wf);
void writeLine(CSC2100::WriteFile* wf, CSC2100::String* line);
void close(CSC2100::WriteFile* wf);

I'll go ahead and include String.h as it is mentioned in the error. Forward declaration of the struct in String.cpp:

namespace CSC2100
{
   struct String
   {
      const char* text;
      int sz; 
   };
}

and String.h:

#if !defined STRING_STRUCT
#define STRING_STRUCT

namespace CSC2100
{
   struct String;
}

CSC2100::String* createString(const char* char_array);
void displayString(CSC2100::String* str);
void destroyString(CSC2100::String* str);
int length(CSC2100::String* str);
const char* getText(CSC2100::String* str);
int a_to_i(CSC2100::String* str);
float a_to_f(CSC2100::String* str);
CSC2100::String* i_to_a(int number);
CSC2100::String* f_to_a(float number);
int find(CSC2100::String* str, char delimiter, int start);
CSC2100::String* substr(CSC2100::String* str, int start, int end);
int compare(CSC2100::String* str1, CSC2100::String* str2);

The code should all be correct as the professor wrote it. All we are doing is applying a namespace to it. Any help is appreciated Thanks.

  • Your professor did everything right! You've been missing to define the methods he was asking for within this namespace! (OT alternatively: "Lacks sufficient information", "Unclear what you're asking") – πάντα ῥεῖ May 05 '14 at 22:38
  • @πάνταῥεῖ I'm sorry i don't understand your comment. What do you mean by missing to define the methods he was asking for within the namespace? – user3558697 May 05 '14 at 22:42
  • first, you shouldn't use `using namespace std` in header files. Secondly, your post is a little unclear - is your definition of `CSC2100:String` in the header, or cpp file? You need the entire definition in the header file, so things that include it know how it's supposed to work. – Red Alert May 05 '14 at 22:44
  • Didn't see any of the definitions the compiler's complaining in the code you're showing here? Also consider [include guards](http://en.wikipedia.org/wiki/Include_guard) for any of your header files! – πάντα ῥεῖ May 05 '14 at 22:44
  • Problem solved presumably. I included String.cpp into WriteFile.cpp and it worked. Not 100% sure why though, i guess because that is where the String struct is actually declared. – user3558697 May 05 '14 at 22:46
  • 1
    Don't include `.cpp` files.. Put the entire declaration of the `struct` in the header. Why forward declare the `struct`? – Brandon May 05 '14 at 22:47
  • @user3558697 I just told you why. Did you read my comment? – Red Alert May 05 '14 at 22:48
  • @user3558697 No, no, no, no!!! You solved nothing, but asking for more serious trouble!! **Don't include .cpp files!!** – πάντα ῥεῖ May 05 '14 at 22:48
  • @RedAlert i posted the comment before i refreshed the comments to see new ones. – user3558697 May 05 '14 at 22:49
  • @Brandon it was part of the assignment to forward declare the stuctures into the .cpp. The purpose is so other programmers can't access members of my structs in the driver. – user3558697 May 05 '14 at 22:51
  • @πάνταῥεῖ Okay so i can't include .cpp files. How can i use the String struct in other .cpp's w/o having to put the stuct into the header? – user3558697 May 05 '14 at 22:52
  • @user3558697 Make a declaration in a header file (and include this where the class is referenced), provide the definition (regarding the correct namespace), in a `.cpp` file. – πάντα ῥεῖ May 05 '14 at 22:52
  • @πάνταῥεῖ Thats how i originally had it formatted but i was getting errors. – user3558697 May 05 '14 at 22:55
  • @πάνταῥεῖ i #included 'String.h' in WriteFile.cpp and it didn't compile(gave me the same errors as before), but String.h is included in WriteFile.h so I'm not sure what is going on. – user3558697 May 05 '14 at 23:01
  • 1
    @user3558697 You have to put the full definition of the struct in the header. How else will `WriteFile.cpp` know what the members of `CSC2100::String` are? When you say `line->text`, the compiler has no idea what you're talking about, since you haven't told it that `text` is a member of `CSC2100::String` – Red Alert May 05 '14 at 23:09

1 Answers1

0

This code

namespace CSC2100 {
   struct String {
       const char* text;
       int sz; 
   };
}

certainly should gp to String.h and not to String.cpp!!!

See also: Including .cpp files

For WriteFile.h try (instead of including String.h):

 #include <fstream>

 // #include "String.h" **** OMIT THIS ****
 namespace CSC2100 {
     class String;
 }

 // ...

and include String.h in WriteFile.cpp!

You must use forward declare class declarations (no matter if these are in a particular namespace or not), to resolve these kind of circular references.

Community
  • 1
  • 1
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • There's some flaw then, you're not explaining in your question. Did you get my concerns about _forward class declarations_? You might not be able inlinig code using these in header files, move these to the `.cpp`! – πάντα ῥεῖ May 05 '14 at 23:20
  • I can't find my error. I've sent the lab to the professor so maybe he'll be able to find it. Thanks for all your help though – user3558697 May 05 '14 at 23:27
  • @user3558697 If I would be your profeesor, I'd just deny further advice and send you right back to the start ;) ... – πάντα ῥεῖ May 05 '14 at 23:34