7 Replies - 519 Views - Last Post: 12 February 2010 - 03:08 AM Rate Topic: -----

#1 Toxicterror  Icon User is offline

  • blub

Reputation: 15
  • View blog
  • Posts: 687
  • Joined: 10-March 09

Explanation of a C++ Codeline

Posted 11 February 2010 - 06:59 AM

Hi Guys

first of, the new board looks quiet cool :D more web 2.0 like :P

so my question:
i have some visual C++ 6 project that i just converted in Visual Studio 2008 Pro
i could get rid of some errors but one codeline really is to complicated for me :D (as a non C++ programmer)

// Input/Output functions
        int ostream &operator << (ostream &os, const DSDLIST &dlist);  // Standard output

i get quiet a lot of errors on this line :D but have no idea what to to with them :D

error C2143: syntax error : missing ';' before '&'
error C2327: 'DSDLIST::ostream' : is not a type name, static or enumerator
error C2061: syntax error : identifier 'ostream'
error C4430: missing type specifier - int assumed. Note C++ does not suppport default-int
error C2805: binary 'operator <<'hast to few parameters



so i dont want the solution,
i actually just wanted the whole line explained and maybe a hint, how i can get rid of these errors :D

for some errors you may need more information, if so, just ask for it ;)


would really be glad if i could get this working :D
because in just one project of about 9 i already get 238 Errors ^^

cheerio

This post has been edited by Toxicterror: 11 February 2010 - 07:34 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Explanation of a C++ Codeline

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Explanation of a C++ Codeline

Posted 11 February 2010 - 07:04 AM

This looks like a << operator overload. Is it inside a class? Does the project has 'using namespace std'? Is DSDLIST a class? Post the entire code so we can figure out.
EDIT: here's the link to get you started about I/O operator overloading: http://www.learncpp....e-io-operators/

This post has been edited by sarmanu: 11 February 2010 - 07:09 AM

Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,519
  • Joined: 23-August 08

Re: Explanation of a C++ Codeline

Posted 11 February 2010 - 07:11 AM

int ostream &operator

You CAN'T have two return types for a method! int AND ostream?
Was This Post Helpful? 1
  • +
  • -

#4 Toxicterror  Icon User is offline

  • blub

Reputation: 15
  • View blog
  • Posts: 687
  • Joined: 10-March 09

Re: Explanation of a C++ Codeline

Posted 11 February 2010 - 07:11 AM

yes it is a class
class DSDLIST
its a headerfile called dlist.hxx

no i couldnt find something like "using namespace std"


sorry im not allowed to provide the whole file... but i can give you just some "in-file documentary" from the top of the file ^^

// dlist.hxx
//
// Class definition of Double precision LIST.
// This class represents an array of double precision numbers. In
// general all elements of the array need to be stored. There are
// however, cases in which many of the elements of the list are zero
// (the so-called sparse vectors and matrices), but DLIST should NOT be
// used for these cases.
// We adopt the following Programming by Contract rule: if the client tries
// to create a DLIST object with a negative number of elements, the number of
// elements will be assigned to 1. The same situation holds when we try to create
// an array but there is not enough memory; in other words, an array of length 1 is
// created.
//  
// The main responsibilities are;
//
//      1. It acts as a service class to other classes (e.g. polynomials, matrices)
//      2. Hides memory allocation from client classes
//      3. Hides indexing problems from other classes
//      4. It models 'static' arrays (it does not grow)
//	5. It has facilities for basic linear algebra (BLAS).
// 	6. DLIST objects can be used in optimisation problems.
//
// In short, client classes can perform their duties without being
// involved in low-level details.
//
// ** DSDLIST's server classes: None


Was This Post Helpful? 0
  • +
  • -

#5 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Explanation of a C++ Codeline

Posted 11 February 2010 - 07:22 AM

Lol, I missed that int, thanks Jack.
As for your function, if it is inside the class, I'd rewrite it like that:
friend std::ostream &operator << (std::ostream &os, const DSDLIST &dlist);


friend specifies that the function has access to private member functions/variables of that class
std:: prefix added in front of ostreams, since you don't have 'using namespace std'.
More about using namespace std: http://forums.devshe...mean-45679.html
Was This Post Helpful? 1
  • +
  • -

#6 Toxicterror  Icon User is offline

  • blub

Reputation: 15
  • View blog
  • Posts: 687
  • Joined: 10-March 09

Re: Explanation of a C++ Codeline

Posted 11 February 2010 - 07:33 AM

it seemed to help :D
thanks alot ;)

one final question:
... no actually two ^^

what is the '&' for and what is 'const'?


however.. i actually could google for those two, lol
you both deserved a... wait? arent there any thanks buttons anymore? :D never mind, thanks guys ^^
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,519
  • Joined: 23-August 08

Re: Explanation of a C++ Codeline

Posted 11 February 2010 - 07:39 AM

const is constant, & means -- in this case -- a reference argument.
const DSDLIST &dlist

This means that the object being passed is to be the *actual* object, not a *copy* (that's the reference part). Were it not a reference (or a pointer, which is really what the reference is underneath it all), a copy would be made via the copy constructor.

The const tells the compiler that the object will NOT be modified within the function.
Was This Post Helpful? 1
  • +
  • -

#8 Toxicterror  Icon User is offline

  • blub

Reputation: 15
  • View blog
  • Posts: 687
  • Joined: 10-March 09

Re: Explanation of a C++ Codeline

Posted 12 February 2010 - 03:08 AM

cool thanks ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1