0

I have these two files table.cpp and table.h in my program code apart from the main.cpp. The files are described as below

table.cpp

#include <iostream>
#include "table.h"

using namespace std;

// accessor function for Name
char* PeriodicTable::Name()
{
    return Name;
}

// accessor function for Symbol
char* PeriodicTable::Symbol()
{
    return Symbol;
}

table.h

#ifndef TABLE_H
#define TABLE_H

class PeriodicTable
{
    char Name[15], Symbol[3], GroupName[20], Block, State[25], Colour[15], Classification[20];
    int GroupNo, AtomicNo, PeriodNo;
    float Weight;

public:

    char* Name();
    char* Symbol();


};

#endif

but the problem is that the IntelliSense(since I am using Visual C++ Express 2010) shows a red curved underline below the name and symbol in the accessor function in table.cpp. I can't understand why???

5
  • 1
    "Intelli"sense is a big name... does it compile / link OK? As an aside I don't like having variables and functions with the same name... Commented Dec 12, 2013 at 17:01
  • 2
    IntelliSense is thinking you're returning a reference to a function. Btw, if you hold your mouse over that little red squiggly, it will tell you this. Commented Dec 12, 2013 at 17:03
  • You should move your #include <iostream> statement from your .cpp to your .h file. Commented Dec 12, 2013 at 17:05
  • 3
    @ThomasBenard: You should not, why would you? Commented Dec 12, 2013 at 17:05
  • VS 2010 and above use the EDG compiler front-end for IntelliSense. This is not some ghetto tool. If it throws red squiggles at you it's a good idea to hover your mouse over it. Commented Dec 12, 2013 at 17:14

1 Answer 1

4

Your member functions and member variables have the same name. This is not possible in C++. That's why various conventions exist for naming member variables, e.g. m_name, name_ etc. (NB: When dealing with underscores in identifiers make sure you don't use a reserved name by accident.)

You might wonder why and how that could possibly go wrong. In your example there clearly is no way to invoke operator() on char[15], but the problem is that the compiler only knows that after performing semantic analysis. There could also be cases where it is impossible to disambiguate. For example:

struct Func {
  void operator()() { };
};

struct C {
  Func f;
  void f() {}
};

int main() {
  C c;
  c.f(); // which one?
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.