Okay, I've got two files: a main and a header to go with it.

I'm not too far into it yet, but for some reason I'm having problems with the string. Am I declaring my strings incorrectly? Am I missing a header file from installation of Visual Studio? Am I doing something else wrong?!? Any thoughts/help?! Thanks in advance!


Main:

#endif
#include "main.h" 
#include <string>


int main()
{
	quizz  q;
	cout<<"Welcome to The Geek Quizz!"<<endl;
	//cout<< q.question1<<endl;
	//cout<<q.answers1<<endl;
	system("pause");
	return 0;
}

Header:

#ifndef MAIN_H
#define MAIN_H
#include <iostream>
#include <string>
using namespace std;

class quizz
{
public:
	//void outQ1()
	//{
	//	cout<<question1<<endl;
	//}
	////void outQ2()
	//{
	//	cout<<question2<<endl;
	//}
	////void outQ3()
	//{
	//	cout<<question3<<endl;
	//}
	////void outQ4()
	//{
	//	cout<<question4<<endl;
	//}
	////void outQ5()
	//{
	//	cout<<question5<<endl;
	//}
	
	/*void outA1()
	{
		cout<<answers1<<endl;
	}
	void outA2()
	{
		cout<<answers1<<endl;
	}
	void outA3()
	{
		cout<<answers1<<endl;
	}
	void outA4()
	{
		cout<<answers1<<endl;
	}
	void outA5()
	{
		cout<<answers1<<endl;
	}*/
	
	
	
	
private:
	string question1;
		question1= "What is the net?";
	string question2 = "What is an operator";
	string question3 = "What do chips mean to you?";
	string question4 = "What is a icon?";
	string question5 = "What is a PNG";
	string answers1 = "a) A fishing net in norway\nb) What net?\nc) The Internet\nd) The National Entimology Team";
	string answers2 = "a) A person on a phone\nb) What operator?\nc) A thing that changes your looks\nd) + - * / & %";
	string answers3 = "a) A lunch source\nb) Sillicon chips?\nc) A fattning source\nd) A quick Snack";
	string answers4 = "a) 3 Mile island\nb) What's a Icon?\nc) a .ico file that has pictures in it\nd) a picture";
	string answers5 = "a) A File?\nb) A Portable Network Graphic\nc) A portly nincompoop in graphite\nd) An image";
	char rans1 = 'c';
	char rans2 = 'd';
	char rans3 = 'a';
	char rans4 = 'c';
	char rans5 = 'b';
};

yes, but perhaps you want to type using namespace std; in your files

either that or type std::string blah = "foo"; without knowing what problem you're having, I'm not certain what the solution is.

I have using namespace std in my files....?? Any other ideas?

sure.

what's the error?

Post the error...

>#ifndef MAIN_H
>#define MAIN_H
you put your #endif in main, it should go at the bottom of your header.

Like the others have said - post your specific problem.

This isn't your error, but it's a bad idea to put function implementations in .h files. If you include the same .h file more than once in different files that are within the same projects, you will get issues. The second time the .h is #included, the compiler will attempt to re-define all the functions within it, which will cause an error (even if the definitions are the same as the ones before). What's done a lot is to have one header file (in your case, perhaps quiz.h), that defines the class and has prototypes of all the class's methods, but doesn't actually define the methods themselves. Then, have a .cpp file (in your case, quiz.cpp), that #includes quiz.h, and define all your methods there (you would need to do something like:

void quiz::outQ1()
	{
	     cout<<question1<<endl;
	}

).

Er... as to your problem, I was looking over the code, and I'm not so sure something like string question3 = "What do chips mean to you?"; works. You need to simply define the member string question3; and initialize it in a constructor.

Ok, thank you all for your responses so far.

For starters, my exact error is:
error C2864: 'quizz::rans1' : only static const integral data members can be initialized within a class

It gives this error for every attempted definition of question1, question2, etc. and answer1, answer2, etc., and rans1, rans2, etc. You get the idea...

So, now any clearer ideas from anybody!? Thanks!

//example :
...........

class quizz
{
        private:
             string question1;
        // ..........
        public:
             quizz()
             {
                  question1 = "what is the net";
             }
             void outA()
             {
                   std::cout<<question1
                   <<std::endl;
             }
             // ...............
};

// you can try it..

like they said, you can't initialize variables inside the class declaration.

furthermore, you should use arrays instead of question<number>, like string question[4]; .

Okay, after applying everyone's advice, I now have this:

But the same 16 errors exist...


main:

#include "main.h" 
#include <iostream>
#include <string>
using namespace std;


int main()
{
	quizz  q;
	cout<<"Welcome to The Geek Quizz!"<<endl;
	//cout<< q.question1<<endl;
	//cout<<q.answers1<<endl;
	system("pause");
	return 0;
}

void quizz::outQ1()
{
     cout<<question1<<endl;
}

void quizz::outQ2()
{
	cout<<question2<<endl;
}
void quizz::outQ3()
{
	cout<<question3<<endl;
}
void quizz::outQ4()
{
	cout<<question4<<endl;
}
void quizz::outQ5()
{
	cout<<question5<<endl;
}

void quizz::outA1()
{
	cout<<answers1<<endl;
}
void quizz::outA2()
{
	cout<<answers2<<endl;
}
void quizz::outA3()
{
	cout<<answers3<<endl;
}
void quizz::outA4()
{
	cout<<answers4<<endl;
}
void quizz::outA5()
{
	cout<<answers5<<endl;
}

and the header:

#ifndef MAIN_H
#define MAIN_H
#include <iostream>
#include <string>
using namespace std;



class quizz
{
public:
	void outQ1();
	void outQ2();
	void outQ3();
	void outQ4();
	void outQ5();
	
	void outA1();
	void outA2();
	void outA3();
	void outA4();
	void outA5();
	

private:
	string question1[80] = "What is the net?";
	string question2[80] = "What is an operator";
	string question3[80] = "What do chips mean to you?";
	string question4[80] = "What is a icon?";
	string question5[80] = "What is a PNG";
	string answers1[80] = "a) A fishing net in norway\nb) What net?\nc) The Internet\nd) The National Entimology Team";
	string answers2[80] = "a) A person on a phone\nb) What operator?\nc) A thing that changes your looks\nd) + - * / & %";
	string answers3[80] = "a) A lunch source\nb) Sillicon chips?\nc) A fattning source\nd) A quick Snack";
	string answers4[80] = "a) 3 Mile island\nb) What's a Icon?\nc) a .ico file that has pictures in it\nd) a picture";
	string answers5[80] = "a) A File?\nb) A Portable Network Graphic\nc) A portly nincompoop in graphite\nd) An image";
	char rans1[10] = 'c';
	char rans2[10] = 'd';
	char rans3[10] = 'a';
	char rans4[10] = 'c';
	char rans5[10] = 'b';
};
#endif

What now?? :(

You misunderstood - on two accounts:

This:

string question1[80] = "What is the net?";

is not ok. You can only declare member variables in the class definition (e.g.: string question1[80]; . You cannot define them as you have done. The exception to this rule is when the member is static and integral. For example:

class MyClass
{
    public:
    //....
    private:
    static int identifier = 1;//ok
    double amount = 5.0;//not ok
}

To fix this, do:

class MyClass
{
    public:
    MyClass
    {
        amount = 5.0;//amount is declared below, but defined here, in the constructor
    }
    //....
    private:
    static int identifier = 1;//ok still
    double amount;//ok now
}

The other thing you misunderstood was the array thing.

You have 5 question variables. Instead of doing this, it would be more efficient to do this:

string questions[5];

. So, instead of accessing the second question like this: question2 = "Whatever" , you would do questions[1] = "Whatever" (remember, array indexing begins at 0, not 1, so the second question has an index of 1, not 2).

in math.h

string question1[80];

- this is array of 80 strings, but you need only 1 string, not 80.
so

string question1;

is ok.
but in class you can't define
string question1 = "What is the net?";
this must be done in constructor, so you need only string question1; for now.
create constructor name quizz(), must be public and there you can initialized like this -

question1 = "What is the net?";

... The exception to this rule is when the member is static and integral.

The exception to this rule is when the member is static and integral and const.

The exception to this rule is when the member is static and integral and const.

Oh, ok. To be honest I didn't actually know the exception - I just gave it based on the compiler error the OP gave.

Ooooh, ok, I see what's you all mean. Here's my new code...It compiles now!!
Is it correct now? How would I call one of those in main now though?!? Thanks!

header:

#ifndef MAIN_H
#define MAIN_H
#include <iostream>
#include <string>
using namespace std;



class quizz
{
public:
	quizz()
	{
		questions[1] = "What is the net?";
		questions[2] = "What is an operator";
		questions[3] = "What do chips mean to you?";
		questions[4] = "What is a icon?";
		questions[5] = "What is a PNG";

		answers[1] = "a) A fishing net in norway\nb) What net?\nc) The Internet\nd) The National Entimology Team";
		answers[2] = "a) A person on a phone\nb) What operator?\nc) A thing that changes your looks\nd) + - * / & %";
		answers[3] = "a) A lunch source\nb) Sillicon chips?\nc) A fattning source\nd) A quick Snack";
		answers[4] = "a) 3 Mile island\nb) What's a Icon?\nc) a .ico file that has pictures in it\nd) a picture";
		answers[5] = "a) A File?\nb) A Portable Network Graphic\nc) A portly nincompoop in graphite\nd) An image";

		ranswers[1] = 'c';
		ranswers[2] = 'd';
		ranswers[3] = 'a';
		ranswers[4] = 'c';
		ranswers[5] = 'b';
	}

	void outQ1();
	void outQ2();
	void outQ3();
	void outQ4();
	void outQ5();
	
	void outA1();
	void outA2();
	void outA3();
	void outA4();
	void outA5();
	

private:
	string questions[5];
	string answers[5];
	char ranswers[5];
};
#endif

main:

#include "quizz.h" 
#include <iostream>
#include <string>
using namespace std;


int main()
{
	//quizz  q;
	cout<<"Welcome to The Geek Quizz!"<<endl;
	system("pause");
	return 0;
}

void quizz::outQ1()
{
     cout<<questions[1]<<endl;
}

void quizz::outQ2()
{
	cout<<questions[2]<<endl;
}

void quizz::outA1()
{
	cout<<answers[1]<<endl;
}

Is it correct now?

Not quite, array indexes start from zero, not from one, i.e. you have to change

// from 
questions[1] = "What is the net?";
// to 
questions[ 0 ] = "What is the net?";

// and so on ...

To call one of these functions, you first need to create an object of the quiz class. The constructor is automatically called when you do this. Then you could call one of your methods using the dot operator.

quiz myQuiz;
myQuiz.outQ1();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.