Solved

Really really simple question about member functions in classes

Posted on 2007-11-26
2
186 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
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

840 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