External library text game error

I'm supposed to write a text adventure game that makes use of an external library, but I ran into a bit of a snag. I got "error: expected primary-expression before 'struct'" on the line that mentions "knifeDamage(struct lizard);"

1
2
3
4
5
6
7
8
#include <iostream>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
struct lizard;
void knifeDamage(lizard);
int main()


snipping 138 lines of plot unrelated to my question.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    cout << "\n!!!----------------------Versus: ZX-71JARVIAN----------------------!!!" << endl;
    cout << endl;
    cout << "Prepare to die, earthling scum!" << endl;
    cout << endl;
    int attack;
    cout << "# What is your first move?" << endl;
    cout << "\t Enter 1 for knife attack" << endl;
    cout << "\t Enter 2 for gun attack" << endl;
    cout << "\t Typing anything else is treated as doing nothing" << endl;
    cin >> attack;
    if(attack == 1)
    {
        cout << "Hit opponent for 5 damage!" << endl;
        cout << endl;
        knifeDamage(struct lizard);
    }
}


And here is the contents of the library:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
extern "C"
{
    struct lizard
    {
        int health;
        int damage;
    };
    lizard mylizard;
    void knifeDamage(lizard);
    int main()
    {
        mylizard.health = 20;
        mylizard.damage = 5;
    }
    void knifeDamage(struct lizard)
    {
        mylizard.health = (mylizard.health - 5);
    }
}


Can you help me?
Line 15 :
knifeDamage(struct lizard);

That is not how a function is called. So if your variable is 'mylizard' then I would call the function like this :

knifeDamage(mylizard);

And here is the contents of the library

Your professor gave you that? Honestly this code is actually full of problems.
My professor did not give me any of this actually. The assignment was to do it from scratch.

I changed it to what you told me and the error now instead reads "'mylizard not declared in this scope"

When you say this code is full of problems, what problems do you mean?
Last edited on
If you are having compiler errors, post your whole code so that to help others compile your program. They will point out the errors for you.

Also, I am confused there are two functions main(). The library you show us is not a library and I have no idea what you are achieving with this code.

1
2
3
4
5
int main()
{
    mylizard.health = 20;
    mylizard.damage = 5;
}


So honestly does what this function do?
Last edited on
This function hardcodes the values for health and damage to 20 and 5 respectively.

I am not getting any errors in the library itself, only in the main program. Sorry if I didn't make that clear. As for not showing the whole code, do you really want to read 138 lines of cout << "some dialogue" << endl;? I did you a favor man.

Shoot, it's after midnight. Can we continue this thread in the morning? I need sleep.
mylizard not declared in this scope


Your library function knifeDamage does nothing with its parameter. It only modifies the object named mylizard. It hasn't been declared yet, but it is defined in the library.

If you really intend to modify that single global object, declare
extern lizard mylizard;
At appropriate scope in each translation unit. It's conventional to put these declarations of global objects in the library's header file.
Last edited on
That quadrupled the number of errors. Forgive me if I don't take that approach.
Forgive me if I don't take that approach.

You have nothing to be sorry for. However, you should be aware that the claim "it doesn't work" is useless and won't get you help.
http://www.catb.org/~esr/faqs/smart-questions.html

A more useful response would be to post
a.) a minimal working example (MWE)
b.) the error messages you received when compiling that MWE
c.) your compiler command line for that MWE

So that someone can tell you what's wrong.
Last edited on
Your main.cpp does appear to include your lizard.h header. You need to do this so functions in main can see the library declarations.

lizard.h:
Line 1: Why are you declaring C linkage (extern "C")?

Line 8: You should not be declaring a global (mylizard) in your header file. This will cause problems if your header file is included in more than one compilation unit.

Lines 10-14: You can't have two main() functions. If you want to initialize a lizard object, use a constructor.

This compiles cleanly:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//  lizard.h
struct lizard
{   int health;
    int damage;
    
    lizard ()   //  Constructor
    {   health = 20;
        damage = 5;
    }      
    
    void knifeDamage ()     //  Member function
    {   health -= 5; 
    }          
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//  main.cpp
#include <iostream>
#include "lizard.h"
using namespace std;

int main()
{   lizard  mylizard;

    cout << "\n!!!----------------------Versus: ZX-71JARVIAN----------------------!!!" << endl;
    cout << endl;
    cout << "Prepare to die, earthling scum!" << endl;
    cout << endl;
    int attack;
    cout << "# What is your first move?" << endl;
    cout << "\t Enter 1 for knife attack" << endl;
    cout << "\t Enter 2 for gun attack" << endl;
    cout << "\t Typing anything else is treated as doing nothing" << endl;
    cin >> attack;
    if(attack == 1)
    {   cout << "Hit opponent for 5 damage!" << endl;
        cout << endl;
        mylizard.knifeDamage ();    //  Call member function 
    }
    return 0;
}




Last edited on
Erm, what? "lizard.h" isn't even an actual file. I'm not using a header file, only a main program (textgame.cpp) and external library (shared library under the name "monster").
Then what is the header declaring lizard structure that you forgot to include? Also you don't pass a structure but an instance as parameter.
(shared library under the name "monster")

To use a symbol, it must be declared before its first use.

As I mentioned, it's conventional to have a header file associated with a library which contains forward declarations of every symbol with external linkage. The definitions of the symbols should appear in the library itself.

This was the header file I was referring to. Create a monster.h which contains forward declarations of the externally-linked symbols in libmonster.so.

Include that file from textgame.cpp.
Get a dynamic library handle using dlopen(3) from <dlfcn.h>.
Use dlsym(3) to obtain pointers to the symbols you need.
That's it.

You can find a decent example, here:
http://stackoverflow.com/a/497158
Plenty more are accessible with some searching.

P.S.:
"external libraries" aren't necessarily dynamic. Static libraries don't require C linkage (because the symbols are visible to the linker), and the so-called "header-only libraries" compile directly into the TUs (translation units) they're included in.

P.P.S.:
Maybe the cppreference pages on storage classes and language linkage will help:
http://en.cppreference.com/w/cpp/language/storage_duration
http://en.cppreference.com/w/cpp/language/language_linkage
Last edited on
Erm, what? "lizard.h" isn't even an actual file. I'm not using a header file, only a main program (textgame.cpp) and external library (shared library under the name "monster").

As mbozzi pointed out, you need a header file to declare the interface to your external library, otherwise, how does main() know how to call the external library?

Since you didn't indicate the name of the header file, I had to call it something. Since the main attribute of your header was the lizard structure, I called the header "lizard.h". You can call it anything you want, but you have to have a #include for it in main (textgame.cpp).




Topic archived. No new replies allowed.