Search code examples
c++objectscopeprogram-entry-point

c++ class not declared in scope - called from main


been stuck for quite a while now. Any help would be much appreciated.

FlexStringDemo.cpp - main

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

using namespace std;

int main(){
    FlexString inputOne;
    return 0;
}

FlexString.h

#ifndef FLEXSTRING_H
#define FLEXSTRING_H

#include "LinkedList.h"
namespace linkedList{

class FlexString{

    public:
        FlexString();
        FlexString(std::string input);
    private:
        LinkedList *list;
        std::string data;

};
}
#endif

FlexString.cpp

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

namespace linkedList{

FlexString::FlexString(){
    list = new LinkedList();
}

FlexString::FlexString(string input){
    list = new LinkedList();
}

}

I get this error message:

FlexStringDemo.cpp: In function ‘int main()’:

FlexStringDemo.cpp:14:2: error: ‘FlexString’ was not declared in this scope FlexString inputOne; ^ FlexStringDemo.cpp:14:2: note: suggested alternative: In file included from FlexStringDemo.cpp:1:0: FlexString.h:7:7: note: ‘linkedList::FlexString’ class FlexString{

No idea why i cant create a FlexString object as i have included the header file. Thanks in advance


Solution

  • FlexString belongs to the namespace linkedList.

    You have to use scope resolution to access the class, like this

    linkedList::FlexString inputOne;
    

    Or the using keyword to omit the namespace

    using namespace linkedList;
    

    Though I recommend against the latter. Writing the namespace explicity is best.