9 Replies - 4414 Views - Last Post: 27 July 2009 - 01:18 PM Rate Topic: -----

#1 Sonastylol  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 151
  • Joined: 15-December 07

struct as a function parameter?

Posted 26 July 2009 - 05:26 PM

I can't get this to work

#include <iostream>

using namespace std;

int printInformation(struct person structPassedIn)
{
	cout << "printInfo" << endl;
	cout << structPassedIn.age << endl;
	cout << structPassedIn.name << endl;
	return 0;
}

int main()
{
	cout << "Hello World!" << endl;

	struct person 
	{
		int age;
		string name;
	};
	
	person p1;
	p1.age = 50;
	p1.name = "Jeremy";

	//printInformation();
	system("PAUSE");
	return 0;
}



My errors:

[color=#FF0000]
1>directory (8) : error C2027: use of undefined type 'person'
1>directory (5) : see declaration of 'person'
1>directory (8) : error C2228: left of '.age' must have class/struct/union
1>directory (9) : error C2027: use of undefined type 'person'
1>directory (5) : see declaration of 'person'
1>directory (9) : error C2228: left of '.name' must have class/struct/union
1>Build log was saved at "file://c:\Users\Jeremy\Documents\Visual Studio 2008\Projects\Test\Test3\Test3\Debug\BuildLog.htm"
1>Test3 - 4 error(s), 0 warning(s)


I thought I did everything correctly.

Also, can you please explain to me how to return structs, once the passing as a parameter works at least. I'm learning Winsock at the moment, which has a heavy struct use, so I need to learn this stuff.

This post has been edited by Sonastylol: 26 July 2009 - 05:27 PM


Is This A Good Question/Topic? 0
  • +

Replies To: struct as a function parameter?

#2 apw5020  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 666
  • Joined: 26-March 09

Re: struct as a function parameter?

Posted 26 July 2009 - 05:35 PM

You need to put your struct outside of main.
Was This Post Helpful? 0
  • +
  • -

#3 Sonastylol  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 151
  • Joined: 15-December 07

Re: struct as a function parameter?

Posted 26 July 2009 - 05:37 PM

Why is this compiling and running fine, but when I uncomment the second cout line it gives an error?

#include <iostream>

using namespace std;

struct person 
{
		int age;
		string name;
};

int main()
{
	cout << "Hello World!" << endl;
	person p1;
	p1.age = 22;
	p1.name = "Jeremy";
	
	cout << p1.age << endl;
	//cout << p1.name << endl;
	
	system("PAUSE");
	return 0;
}




Theres pages of errors so here is a snippet..

1>		c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(308): or	   'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(328): or	   'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__w64 unsigned long)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(349): or	   'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__int64)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(369): or	   'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned __int64)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(390): or	   'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(float)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(410): or	   'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(double)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(430): or	   'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long double)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(450): or	   'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(const void *)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(470): or	   'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_streambuf<_Elem,_Traits> *)'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>
1>		]
1>		while trying to match the argument list '(std::ostream, std::string)'


This post has been edited by Sonastylol: 26 July 2009 - 06:02 PM

Was This Post Helpful? 0
  • +
  • -

#4 Guest_c.user*


Reputation:

Re: struct as a function parameter?

Posted 26 July 2009 - 07:06 PM

it's alright for uncommented
Was This Post Helpful? 0

#5 Sonastylol  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 151
  • Joined: 15-December 07

Re: struct as a function parameter?

Posted 26 July 2009 - 07:23 PM

View Postc.user, on 26 Jul, 2009 - 06:06 PM, said:

it's alright for uncommented



why is visual studio flipping out, then?
Was This Post Helpful? 0
  • +
  • -

#6 UG Cyber  Icon User is offline

  • D.I.C Addict

Reputation: 32
  • View blog
  • Posts: 551
  • Joined: 24-July 08

Re: struct as a function parameter?

Posted 26 July 2009 - 07:54 PM

View PostSonastylol, on 26 Jul, 2009 - 06:23 PM, said:

View Postc.user, on 26 Jul, 2009 - 06:06 PM, said:

it's alright for uncommented



why is visual studio flipping out, then?


Because its made by microsoft. Failure is not an option, it comes bundled with Microsoft.......
Try this

cout << p1.name.c_str() << endl;


[edit]
the extension .c_str() is a function in the string class that returns a char * value of the string.

This post has been edited by UG Cyber: 26 July 2009 - 07:55 PM

Was This Post Helpful? 0
  • +
  • -

#7 sscheider  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 55
  • Joined: 14-May 09

Re: struct as a function parameter?

Posted 26 July 2009 - 08:01 PM

View PostSonastylol, on 26 Jul, 2009 - 07:23 PM, said:

why is visual studio flipping out, then?

You need an include:
#include <string>


Then it will work uncommented.
Was This Post Helpful? 0
  • +
  • -

#8 sscheider  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 55
  • Joined: 14-May 09

Re: struct as a function parameter?

Posted 26 July 2009 - 08:10 PM

for clarity, here is my code of your app. I built it in MS VC++ 2008 Express, as a Win32 console app. I erase the tmain that it provided.
// goop12.cpp : Defines the entry point for the console application.
//

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

struct person 
{
		int age;
		string name;
};

int main()
{
	cout << "Hello World!" << endl;
	person p1;
	p1.age = 22;
	p1.name = "Jeremy";
	
	cout << p1.age << endl;
	cout << p1.name << endl;
	
	system("PAUSE");
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#9 apw5020  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 666
  • Joined: 26-March 09

Re: struct as a function parameter?

Posted 26 July 2009 - 08:43 PM

View PostUG Cyber, on 26 Jul, 2009 - 10:54 PM, said:

View PostSonastylol, on 26 Jul, 2009 - 06:23 PM, said:

View Postc.user, on 26 Jul, 2009 - 06:06 PM, said:

it's alright for uncommented



why is visual studio flipping out, then?


Because its made by microsoft. Failure is not an option, it comes bundled with Microsoft.......
Try this

cout << p1.name.c_str() << endl;


[edit]
the extension .c_str() is a function in the string class that returns a char * value of the string.


Wrong. As sscheider said, a simple #include <string> will solve the problem.

Any other errors?

This post has been edited by apw5020: 26 July 2009 - 08:45 PM

Was This Post Helpful? 0
  • +
  • -

#10 Aeternalis  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 291
  • Joined: 13-July 09

Re: struct as a function parameter?

Posted 27 July 2009 - 01:18 PM

One other thing to mention on this topic..

As your structures get larger and they will in sockets programming, and possibly if you do anything else with structs, you will want to know that you are passing the struct by value in your simple example. This uses a copy constructor and will create a copy of the existing structure. For very large data structures this ends up being inefficient. For data structures that use pointers, it can cause other problems.

You should pass by reference any time you are passing an object, and passing it as const is good programming practice too if it is not going to be (or shouldn't be) modified by the function it is being passed to.

so in the example you started with.. you could do it like this:


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


 struct person 
	{
		int age;
		string name;
	};


void printInformation(const struct person &);

int main()
{
	cout << "Hello World!" << endl;

	   
	person p1;
	p1.age = 50;
	p1.name = "Jeremy";

	printInformation(p1);
	system("PAUSE");
	return 0;
}

 void printInformation( const person &passedIn)
{
	cout << "printInfo" << endl;
	cout << passedIn.age << endl;
	cout << passedIn.name << endl;
	
}






If I made mistakes in this I appologize .. it has been a while since I coded in C++ and dont have a compiler with me to debug at the moment.

You get the general idea though.. pass by reference not by value for objects and pass as const to keep them from modifying it.

Aet
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1