0

I have a struct

Inside myStructure.h

struct myStructure
{
    int myInteger;
    double myDoublesArray[4];
    char myCharArray[79];

};

Inside myClass.h

#include "myStructure.h"

class myClass
{
private:
    myStructure myStruct[4]

private:
    Prog1Class();
    ~Prog1Class();
    void setMyStructData();
};

Inside main.cpp

#include<iostream>
#include <string>
#include "myClass.h"
#include "myStructure.h"

using namespace std;

void myClass::setMyStructData()
{
    for(int i = 0; i < 5 ; i++)
    {
        cout << "Please enter an integer: " << endl;
        cin >> myStruct[i].myInteger;

        for(int j = 0; j< 5; j++)
        {
            cout << "Please enter a double: ";
            cin >> myStruct[i].myDoublesArray[j];
        }

        cout << endl << "Please enter a string: ";
        cin.ignore(256, '\n');
        cin.getline(myStruct[i].myCharArray, 79, '\n');
    }
}

int main(void)
{
    setStructData();

    cin.get()
}

The errors i'm getting are " 'myStructure' : 'struct' type redefinition " , and " left of '.myInteger' must have class/struct/union "

I'm sure it's some simple mistake I've made with the structure, but I've looked around and everything seems to be correct to my noob eyes. Thanks!

And this isn't homework. I'm just trying to get back into programming, and understanding how some different things work, and I'm doing old assignments from other schools. Thanks.

8
  • 1
    You may want to start off adding a semicolon after the definitions of your class and struct to avoid distractions from these missing. Commented Dec 17, 2013 at 20:02
  • 1
    What is Prog1Struct.h, should that be myStructure.h? Commented Dec 17, 2013 at 20:03
  • I added the semi-colons, thanks! Already an answer posted for it, haha. Commented Dec 17, 2013 at 20:03
  • 1
    @Slimmons: well, that's the obvious first error spotted. The actual error is slightly harder to see (missing include guards). ... and I'd bet that adding include guards fixes the actual problem! Commented Dec 17, 2013 at 20:06
  • 1
    @Beta: That is something some professional developers should follow, too! When I my first project there were more than 40 people "programming" since about half a year with ever compiling any of the stuff they had programmed. Once the infrastructure was set up to actually build something all this code went to the bin... Commented Dec 17, 2013 at 20:12

2 Answers 2

4

Your key problem is that you don't have include guards: when including the same header multiple times the compiler sees the same definition multiple times and doesn't like that! You need to guard against that, typically using macros, e.g.:

// myStructure.h
#ifndef INCLUDED_MYSTRUCTURE
#define INCLUDED_MYSTRUCTURE

// struct definition goes here

#endif

(likewise for all other headers).

Sign up to request clarification or add additional context in comments.

4 Comments

Hey, thanks, that fixed almost all of the errors. I'm still getting 1 error though. Inside of my main(), when I call the setStructData() function, it's saying setStructData identifier is not found.
@Slimmons: compilers are annoying and stubbornly read what you write not what you meant! Did you mean to write setMyStructData? Also, to use this member function it needs to be called on an object...
thanks, all problems solved now, I appreciate the help and information.
@clcto Just a note for anybody looking at this later, clcto shows examples of how to fix it. Thanks all.
1

You are getting multiple definitions because you include the structure header in both the myClass.h and main.cpp. You want to use include guards: https://en.wikipedia.org/wiki/Include_guard.

Also in your main() you are trying to access a private member function without having an instance of the class. There are a few issues here. First you need an instance. However you cannot create an instance in main because the constructor is private as well. Your myClass.h should look like this:

class myCLass
{
private:
    myStructure myStruct[4]

public:  // NOTE public here
    Prog1Class();
    ~Prog1Class();
    void setMyStructData();
};

Then in main you can create a myClass:

myClass c;

And then you can call the member function on c (because I changed it to public, otherwise you still would not be allowed to call it):

c.setMyStructureData();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.