We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,939 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Object not declared in this scope(OOP and Header files)?

The code can do the talking.

main.cpp

#include "test.h"

#include <iostream>
#include <string>

#define NL "\n"     //"NL" = "New Line"(move the pointer to the next line)
#define SL "\n\n"   //"SL" = "Skip Line"(move the pointer 2 lines)

int main()
{
    Cool_Object.cool_function();

    return 0;
}

test.h

#ifndef TEST_H
#define TEST_H

class Cool_Class
{
    public:
        void cool_function();
};

#endif

test.cpp

#include "test.h"

#include <iostream>
#include <string>

#define NL "\n"     //"NL" = "New Line"(move the pointer to the next line)
#define SL "\n\n"   //"SL" = "Skip Line"(move the pointer 2 lines)

Cool_Class Cool_Object; //WHY CAN'T THIS WORK!!!

Cool_Object.cool_function()
{
    std::cout << "SPRING!!!" << NL;
}

OK, so if I create the object in "main.cpp" it works, but there must be another way, right. All objects ever created cannot have all been created in the file containing "main()" can it?

The error I get is something like "Cool_Object was not declared in this scope".
So what is the problem here? Is it just Code::Blocks(I use Code::Blocks) that sucks or what? lol...

2
Contributors
1
Reply
37 Minutes
Discussion Span
1 Year Ago
Last Updated
2
Views
Hoff123
Newbie Poster
10 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hi,

Your declaration of member functions is wrong in the test.cpp Should be like void Cool_Class::cool_function() Also, it has been a while since I do OOP in C++ but I think you also need to declare a constructor, so the object knows how to exist...

ItecKid
Junior Poster in Training
72 posts since Dec 2008
Reputation Points: 10
Solved Threads: 7
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.3137 seconds using 2.7MB