non default constructor

im new to this kind of thing sorry im trying to set a member string to the value held to the constructor parameter is that ok or do I need to fix it?

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 #include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;

template<class gamer>
class game
{
private:

protected:
	int nintendo;
	int sega;
	string fun;
	bool enjoy;
public:

};

template<class gamer>
class game2 : public game<gamer>
{
private:
	string joy;
protected:

public:
	void jello(string joy)
	{
		jello = joy;
	}
	string joy()
	{
		return jello;
	}
};

int main()
{

}
this looks off to me.

style issue: I prefer everything to have a unique name. Its ok to reuse names (C++ allows it) but its really easy to make a mess this way.

jello... where is that defined? I see a function jello, but
jello = joy; //what is jello here??? Its not the function?



what can i do
not 100% sure what you want to do.

lets take it one thing at a time.
first, please, please, have mercy and name stuff uniquely.

you can use better names than I do here but anything is better than name collision... eg:
void vjello(string joy_in)
{
jello = joy_in;
}
string sjoy()
{
return jello;
}

and then you can add this
string jello;
under the private parts of your class.

If that does not work, repost your new code and ask again, explaining exactly what you want it to do and we will get it working.

#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;

template<class gamer>
class game
{
private:

protected:
int nintendo;
int sega;
string fun;
bool enjoy;
public:

};

template<class gamer>
class game2 : public game<gamer>
{
private:
string joy;
string jello;
protected:

public:
void vjello(string joy_in)
{
jello = joy_in;
}
string sjoy()
{
return jello;
}
};

int main()
{

}

how do i create a vector of objects i want to create 5 in the subclass thanks for helping
closed account (E0p9LyTq)
ow do i create a vector of objects i want to create 5


Include the <vector> header, and then std::vector<object_name> aVector(5);

You now have a vector with 5 elements capable of storing your objects.

No need to push back, simply access using standard operator[] notation, 0 -4.

The space for the 5 objects is allocated, you have to provide values for each.
Last edited on
where do I create the objects and how do I pass in a string to the object's constructor sorry

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

template<class gamer>
class game
{
private:

protected:
	int nintendo;
	int sega;
	string fun;
	bool enjoy;
public:

};

template<class gamer>
class game2 : public game<gamer>
{
private:
	string joy;
	string jello;
protected:

public:
	void vjello(string joy_in)
	{
		jello = joy_in;
	}
	string sjoy()
	{
		return jello;
	}
	vector<cookies> aVector(5);
};

int main()
{

}
Why don't you explain in plain English what you want to do.
For example the use of template makes no sense since you don't use it.
Also it would be better if you have meaningful and consistent names
vector<cookies> aVector(5);
What are cookies?
where do I create the objects and how do I pass in a string to the object's constructor sorry

Lets try an example:
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <vector>
#include <string>

class Foo
{
  std::string word;
public:
  Foo( const char* w );
  friend std::ostream & operator<< ( std::ostream &, const Foo& );
};

std::ostream & operator<< ( std::ostream & out, const Foo& foo )
{
  out << foo.word;
  return out;
}

Foo::Foo( const char* w )
 : word( w )
{
}

class Bar
{
  std::vector<Foo> foos;
public:
  Bar();
  void print() const;
};

Bar::Bar()
 : foos( {"Hello", "Dolly", "world"} )
{
}

void Bar::print() const
{
  for ( auto f : foos ) std::cout << f << ' ';
}

int main()
{
  Bar bar;
  bar.print();
}

We do create one object on line 44. We do use its default constructor, but we have rewritten that constructor to initialize member vector of Bar with three words.

The member vector of Bar has elements of type Foo. Foo has non-default constructor that uses its argument to initialize the string member of Foo.
what should I put in main sorry im doing homework i need help for it trying to do what keskiverto did

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

template<class gamer>
class game
{
private:

protected:
	int nintendo;
	int sega;
	string fun;
	bool enjoy;
public:

};

ostream & operator ( ostream & out, const game);

template<class gamer>
class game2 : public game<gamer>
{
private:
	string joy;
	string jello;
protected:

public:
	void vjello(string joy_in)
	{
		jello = joy_in;
	}
	string sjoy()
	{
		return jello;
	}
	vector<cookies> gamingg(5);
};

gamingg::game()
{

}

int main()
{
	

}
In main, you declare your game2 object, and then call the methods on it that you want to call.

Why did you ignore the questions that people have been asking you?
i made 5 objects right or did i do something wrong

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class game2 : public game<gamer>
{
private:
	string joy;
	string jello;
protected:

public:
	void vjello(string joy_in)
	{
		jello = joy_in;
	}
	string sjoy()
	{
		return jello;
	}
	vector<cookies> gamingg(5);
	Tasty();
	Delicious();
	Yummy();
	Hungry();
	Full();
};
No, that doesn't make sense.

Thomas1965 wrote:
vector<cookies> aVector(5);

What are cookies?


If cookies is a class with, let's say, a string in it, you can do something like this:
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
26
#include <iostream>
#include <string>
#include <vector>

class cookies {
  public:
    cookies() {}
    cookies(std::string description)
    : description(description)
    { }
    
    std::string description;
};

int main() {
	
	std::vector<cookies> vec(5);
	
	vec[0] = cookies("Tasty");
	vec[1] = cookies("Delicious");
	vec[2] = cookies("Yummy");
	vec[3] = cookies("Hungry");
	vec[4] = cookies("Full");
	
	return 0;
}


But you're making it hard for anyone to help you, because you are ignoring people's questions.

Last edited on
But you're making it hard for anyone to help you, because you are ignoring people's questions.

They're even ignoring the questions about why they're ignoring questions.

It seems they're only interested in copying and pasting chunks of broken code into the thread, until someone just writes the code for them, so they can hand it in.
This guy's insane.
sorry if im not asking questions im scared its due soon ill try to but im new please forgive me
about the cookies i thought i could put anything in there but its the class sorry a little stressed

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

template<class gamer>
class game
{
private:

protected:
	int nintendo;
	int sega;
	string fun;
	bool enjoy;
public:

};

ostream & operator ( ostream & out, const game);

template<class gamer>
class game2 : public game<gamer>
{
private:
	string joy;
	string jello;
protected:

public:
	void vjello(string joy_in)
	{
		jello = joy_in;
	}
	string sjoy()
	{
		return jello;
	}
	vector<game2> gamingg(5);
	Tasty();
	Delicious();
	Yummy();
	Hungry();
	Full();
	
};

int main()
{
	
}


Last edited on
Your game2 HAS 5 game2's. Each of them HAS 5 game2's. Each of them HAS 5 game2's. Each of them HAS 5 game2's. Each of them HAS 5 game2's. Each of them HAS 5 game2's. Each of them HAS 5 game2's. Each of them HAS 5 game2's. ...

That is infinite recursion. That is not possible.


Lines 42-46 are syntactically function calls. That is not possible in class definition.


Which operator do you declare on line 22? Whitespace is not a valid operator.


Why are you sorry that you don't ask questions? Others have pointed out that you don't answer questions. Totally different problem.
can I make another subclass? sorry if i dont answer questions
is this ok or no?

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

template<class gamer>
class game
{
private:

protected:
	int nintendo;
	int sega;
	string fun;
	bool enjoy;
public:

};

template<class gamer>
class cookies : public game<gamer> {
public:
	cookies() {}
	cookies(std::string description)
		: description(description)
	{ }

	std::string description;
};
int main()
{    
	template<class gamer>
	std::vector<cookies> vec(5);
	template<class gamer>
	vec[0] = cookies("Tasty");
	template<class gamer>
	vec[1] = cookies("Delicious");
	template<class gamer>
	vec[2] = cookies("Yummy");
	template<class gamer>
	vec[3] = cookies("Hungry");
	template<class gamer>
	vec[4] = cookies("Full");

	return 0;
}
how do I print the subclass objects and the int nintendo int sega string fun and bool enjoy?
i like games im almost done the assignment
Topic archived. No new replies allowed.