More C++ Help Please! Header file not functioning correctly! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

More C++ Help Please! Header file not functioning correctly!

I know that this is technically not the correct way to use header files, but all I want is for my code to work and compile correctly. I always appreciate the help. I get the error that the Allele objects are not defined as type. #ifndef STRUCTURE_H #define STRUCTURE_H #include <iostream> using namespace std; class Allele{ public: string phenotype; bool isDominant; }; class Genotype { public: bool isDominant; void makeGenotype(const Allele& firstAllele, const Allele& secondAllele) { if (firstAllele.isDominant) { isDominant = true; cout << "The animal is, " + firstAllele.phenotype; } if (secondAllele.isDominant) { isDominant = true; cout << "The animal is, " + secondAllele.phenotype; } } }; Allele HairyDominant; Allele HairyRecessive; Allele GiantHeadDominant; Allele GiantHeadRecessive; Allele EyeColorOrangeDominant; Allele EyeColorOrangeRecessive; HairyDominant.phenotype="Hairy"; HairyDominant.isDominant= true; HairyRecessive.phenotype=" Not Hairy"; HairyRecessive.isDominant= false; GiantHeadDominant.phenotype="Giant Head"; GiantHeadDominant.isDominant= true; GiantHeadRecessive.phenotype= "Not Giant Headed"; GiantHeadRecessive.isDominant= false; EyeColorOrangeDominant.phenotype= "Not Orange Eyed"; EyeColorOrangeDominant.isDominant=true; EyeColorOrangeRecessive.phenotype= "Orange Eye"; EyeColorOrangeRecessive.isDemoninat=false; Genotype Hairy; Genotype GiantHead; Genotype OrangeEyes; #endif

25th Sep 2018, 12:26 AM
George Ryan
George Ryan - avatar
3 Answers
+ 2
George Ryan where did you assign all the values? They're supposed to be done in the main function.
25th Sep 2018, 9:32 AM
Hoàng Nguyễn Văn
Hoàng Nguyễn Văn - avatar
+ 2
You are assigning values in a header file, which is illegal. Either: 1) Initialize the object when it is instantiated, using constructor. 2) Assign values to the object fields in the source file.
25th Sep 2018, 2:41 AM
Hoàng Nguyễn Văn
Hoàng Nguyễn Văn - avatar
+ 2
I'm used to python syntax so I put it above the main function.
25th Sep 2018, 9:39 AM
George Ryan
George Ryan - avatar