struct with multiple objects in header file issues
Problem: I can't get this seemingly simple program to compile. I've tried searching for hours. It's a struct in a .h file along with a function prototype and its src file that uses the function to put values to the struct's objects. I'm using the Code::Blocks v10.05 compiler.
Compiler errors:
-------------------------
E:\My own programs\structure test\struct practice\fruit_struct.h|12|error: variable or field 'apple_f' declared void|
E:\My own programs\structure test\struct practice\fruit_struct.h|12|error: expected primary-expression before ')' token|
||=== Build finished: 2 errors, 0 warnings ===|
fruit_struct.h file
Code:
#ifndef GUARD_fruit_struct_h
#define GUARD_fruit_struct_h
// fruit_struct.h
#include <string>
struct fruit {
std::string name;
int weight;
float price;
} apple, banana, melon;
void apple_f(apple&);
#endif
fruit_struct.cpp
Code:
// fruit_struct.cpp
#include <string>
#include "fruit_struct.h"
void apple_f(apple& apple)
{
apple.name = "Apple";
apple.weight = 2;
apple.price = 2.0;
}
main.cpp
Code:
// main.cpp
#include <iostream>
#include <string>
#include "fruit_struct.h"
using namespace std;
int main()
{
cout << apple.name;
return 0;
}
I know I could put this all in one source file and be done, but it's not what I'm trying to accomplish doing this exercise.
Any help will be appreciated and noted!