0

In a data-structure, how do you insert a function?

struct Student_info {
std::string name;
double midterm, final;
unsigned int& counter;
std::vector<double> homework;
double overall = grade(students[counter]);
};

always get this type of error:-

a. "variable" was not declared in this code.

b. "Student_info::counter" cannot appear in a constant-expression.

c. an array reference cannot appear in a constant-expression.

d. a function call cannot appear in a constant-expression

edit:- oopps, i mean student_info contain in a vector, wait, why that's info needed anyway... Dx

oh, and btw, this is from Accelerated C++, a book obviously, and I'm trying to answer one of its exercise, then I need to know this part, not found any on the book Dx

the question is 4-6. Rewrite the Student_info structure to calculate the grades immediately and store only the final grade.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Vastor
  • 77
  • 1
  • 3
  • 12
  • 2
    "Student_info is a vector". No it's not. It's a `struct`. – jrok Dec 06 '11 at 11:31
  • I think you need to be a bit more specific about what you are trying to do here. double overall = grade(students[counter]); looks very wrong, shouldnt you be doing something like void set_grade(){ overall = students[counter]; } – Stowelly Dec 06 '11 at 11:33
  • @Vastor I think you need to take a look at a C++ book to get the concepts, then return and ask here. – AndersK Dec 06 '11 at 11:34
  • lol, in another question posted, I explain too much on what to do, and the question got closed, and told to narrow the question... Dx – Vastor Dec 06 '11 at 11:35
  • 2
    I think the other question was closed as it was not a real queston just a mess of information. This is different in that you are obviously coming from a less strict language and have not yet grokked the fundamental syntax of C++. This makes it hard to understand your question as you are mixing the syntax of your original language and C++ in such a way that it makes the question nearly imposable to understand. – Martin York Dec 06 '11 at 11:42

1 Answers1

4

You can NOT dynamically insert a function into a structure.

You can declare a structure that has a method()

struct Student_info
{
    void doDomethingToStudent()
    {
         // Manipulate the object here.
    }
    // STUFF
};

Also you can not initialize member like above.

double overall = grade(students[counter]);

Here you need to create constructor that will initialize members.

struct Student_info
{
    Student_info(std::string& studentName, unsigned int& externalCounter)
        : name(studentName)
        , midterm(0)
        , final(0)
        , counter(externalCounter)
        , homework()

        // It is not clear if overall is a normal memeber
        // Or a static member of the class
        , overall(grade(students[counter]))
    {}
    // STUFF
};
int main()
{
    unsigned int counter   = 0;
    Student_info bob("Bob", counter);
}
Martin York
  • 257,169
  • 86
  • 333
  • 562
  • good, the first answer is what I'm searching for, also, how about if I'm not learned constructor yet? are there any alternative? – Vastor Dec 06 '11 at 11:56
  • Constructors/Destructor are nearly always in the same chapter as your first first introduction to a class (A class is the same as a structure) they are that fundamental. Go back and read chapter 1/2 – Martin York Dec 06 '11 at 12:08
  • wait, did we are talking about a same book? I'm lost about what are you talking about :s – Vastor Dec 06 '11 at 12:15
  • @Vastor: If your book does not talk of constructor/destructor in first 1/2 chapters then you should better throw it away. Pick up one of [these](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Alok Save Dec 07 '11 at 03:56
  • well, Accelerated C++ is one of the book, which I have, and If never read it, unlike other C++ book/tutorial, which define each tools, but teach how to use it (like in art/drawing class, rather than teach to know each color, the book teach oh how to use it instead(color theory etc)), btw, I use learncpp.com to know each tools in c++(thought some claim that it contain nothing about vector(luckily Accelerated C++ has it on chapter 3)) – Vastor Dec 08 '11 at 01:19
  • btw, I'm beginner's that start programming with C++, which the person in your given link claim the book has steep learning curve, well he's right, I need to read twice each chapter, yet, still misinformed on some part. – Vastor Dec 08 '11 at 01:21
  • oh, and btw, thnx for your link, I think I'm gonna get the other book too, like C++ FAQ.. and maybe drop the leanrcpp.com.. =S – Vastor Dec 08 '11 at 01:34