Solved

Really really simple question about member functions in classes

Posted on 2007-11-26
2
188 Views
Last Modified: 2013-12-14
I mean really really simple.

All i want to do is make a class (in Visual C++ 2008) A  that has two int members b and c, and a function d  that adds them together. What i wrote is this:

-------------------A.h--------------------------
#ifndef A_H
#define A_H
#pragma once

class A
{
public:
      int b;
      int c;
      int d();
};
#endif
------------------------------------------------

-----------------A.cpp-------------------------
#include "A.h"

int d()
{
      int r = b + c;
      return r;
}
---------------------------------------------------

With this it tells me that in A.h, b and c are undeclared identifiers.
With A::b and A::c in A.h instead of just b and c it says "error C2597: illegal reference to non-static member 'A::b'" and i don't want them to be static members.

Anyone tell me what the correct method of doing this is?

Thanks a lot for your help.
James

0
Comment
Question by:jsab500
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 200 total points
ID: 20352212

Hi James.

easy fix:



int <classname>:: d()
{
  int r = b + c;
  return r;
}



Good Luck,
kent
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 20352221
That translates to:

int A::d()
{

}



Good Luck,
Kent
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question