Newbie to C++ and visual studio encounters syntax errors during compilation.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: Newbie to C++ and visual studio encounters syntax errors during compilation.

  1. #1
    Join Date
    Jul 2017
    Posts
    10

    Newbie to C++ and visual studio encounters syntax errors during compilation.

    Hello Everyone!

    I'm new to this forum. Coming from c and java, i thought is about time to learn c++. I started learning about a day ago and today i encountered some syntax errors which i can't identify and fix.

    I'm trying to make a simple trie data structure for storing strings. I know exactly how they work, i already made other projects in java about tries, the problem is why i get these errors.

    So lets show you my code (I'm working in visual studio community 2017):

    If you want for simplicity you can download the Visual Studio Project.

    Main.cpp
    Code:
    #include <iostream>
    #include "trie_node.h"
    
    int main(void) {
    
    	system("pause");
    	return 0;
    }
    trie_node.h
    Code:
    #pragma once
    #include "map.h"
    
    class TrieNode {
    
    private:
    	Map map;
    	bool end_of_world;
    
    public:
    	TrieNode();
    	Map getMap();
    	void setBool(bool value);
    	bool getBool();
    
    };
    trie_node.cpp
    Code:
    #include "trie_node.h"
    
    TrieNode::TrieNode() {
    	this->end_of_world = false;
    }
    
    Map TrieNode::getMap() {
    	return this->map;
    }
    
    void TrieNode::setBool(bool value) {
    	this->end_of_world = value;
    }
    
    bool TrieNode::getBool() {
    	return this->end_of_world;
    }
    map_node.h
    Code:
    #pragma once
    #include "trie_node.h"
    
    class MapNode {
    
    private:
    	char ch;
    	::MapNode *next;
    	TrieNode *trie_node;
    
    public:
    	MapNode();
    	char getChar();
    	::MapNode *getNext();
    	TrieNode *getTrie();
    	void setNext(::MapNode *new_node);
    	void setCh(char ch);
    	void setTrie(TrieNode *new_node);
    };
    map_node.cpp
    Code:
    #include "map_node.h"
    
    MapNode::MapNode() {
    	
    	this->ch		= '\0';
    	this->next		= nullptr;
    	this->trie_node = nullptr;
    }
    
    char MapNode::getChar() {
    	return this->ch;
    }
    
    MapNode *MapNode::getNext() {
    	return this->next;
    }
    
    TrieNode *MapNode::getTrie() {
    	return this->trie_node;
    }
    
    void MapNode::setNext(MapNode *new_node) {
    	this->next = new_node;
    }
    
    void MapNode::setCh(char ch) {
    	this->ch = ch;
    }
    
    void MapNode::setTrie(TrieNode *new_node) {
    	this->trie_node = new_node;
    }
    map.h
    Code:
    #pragma once
    #include "map_node.h"
    
    class Map {
    
    private:
    	MapNode *head;
    	MapNode *tail;
    	int size;
    
    public:
    	Map();
    	void insert(char ch, TrieNode *new_node);
    	TrieNode *search(char h);
    	int getSize();
    };
    map.cpp
    Code:
    #include "map.h"
    
    Map::Map() {
    
    	this->head = nullptr;
    	this->tail = nullptr;
    	this->size = 0;
    }
    
    void Map::insert(char ch, TrieNode *trie_node) {
    
    	MapNode *new_node = new MapNode();
    	new_node->setCh(ch);
    	new_node->setTrie(trie_node);
    	this->size++;
    
    	if (this->head == nullptr) {
    		this->head = new_node;
    		this->tail = new_node;
    	}
    
    	else {
    		this->tail->setNext(new_node);
    		this->tail = new_node;
    	}
    }
    
    
    TrieNode *Map::search(char ch) {
    
    	MapNode *current = this->head;
    
    	while (current != nullptr) {
    
    		if (current->getChar() == ch)
    			return current->getTrie();
    
    		current = current->getNext();
    	}
    
    	return nullptr;
    }
    
    
    int Map::getSize() {
    	return this->size;
    }

    Visual Studio Error Codes:
    Name:  Capture.jpg
Views: 59
Size:  45.1 KB

  2. #2
    Join Date
    Jul 2017
    Posts
    10

    Re: Newbie to C++ and visual studio encounters syntax errors during compilation.

    If you can't see the image clearly go here

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,700

    Re: Newbie to C++ and visual studio encounters syntax errors during compilation.

    For the sake of ease of testing, I've put it all into one file. This compiles OK
    Code:
    class MapNode;
    class TrieNode;
    
    class Map {
    private:
    	MapNode *head;
    	MapNode *tail;
    	int size;
    
    public:
    	Map();
    	void insert(char ch, TrieNode *new_node);
    	TrieNode *search(char h);
    	int getSize();
    };
    
    class TrieNode {
    private:
    	Map map;
    	bool end_of_world;
    
    public:
    	TrieNode();
    	Map getMap();
    	void setBool(bool value);
    	bool getBool();
    };
    
    class MapNode {
    private:
    	char ch;
    	MapNode *next;
    	TrieNode *trie_node;
    
    public:
    	MapNode();
    	char getChar();
    	MapNode *getNext();
    	TrieNode *getTrie();
    	void setNext(MapNode *new_node);
    	void setCh(char ch);
    	void setTrie(TrieNode *new_node);
    };
    
    Map::Map() {
    	head = nullptr;
    	tail = nullptr;
    	size = 0;
    }
    
    void Map::insert(char ch, TrieNode *trie_node) {
    	MapNode *new_node = new MapNode();
    	new_node->setCh(ch);
    	new_node->setTrie(trie_node);
    	size++;
    
    	if (head == nullptr) {
    		head = new_node;
    		tail = new_node;
    	}
    	else {
    		tail->setNext(new_node);
    		tail = new_node;
    	}
    }
    
    
    TrieNode *Map::search(char ch) {
    	MapNode *current = head;
    
    	while (current != nullptr) {
    		if (current->getChar() == ch)
    			return current->getTrie();
    
    		current = current->getNext();
    	}
    
    	return nullptr;
    }
    
    
    int Map::getSize() {
    	return size;
    }
    
    MapNode::MapNode() {
    	ch = '\0';
    	next = nullptr;
    	trie_node = nullptr;
    }
    
    char MapNode::getChar() {
    	return ch;
    }
    
    MapNode *MapNode::getNext() {
    	return next;
    }
    
    TrieNode *MapNode::getTrie() {
    	return trie_node;
    }
    
    void MapNode::setNext(MapNode *new_node) {
    	next = new_node;
    }
    
    void MapNode::setCh(char ch1) {
    	ch = ch1;
    }
    
    void MapNode::setTrie(TrieNode *new_node) {
    	trie_node = new_node;
    }
    
    TrieNode::TrieNode() {
    	end_of_world = false;
    }
    
    Map TrieNode::getMap() {
    	return map;
    }
    
    void TrieNode::setBool(bool value) {
    	end_of_world = value;
    }
    
    bool TrieNode::getBool() {
    	return end_of_world;
    }
    
    int main(void) {
    
    	//system("pause");
    	return 0;
    }
    From looking at the headers, I think you have a circular reference? From the code above, clssses MapNode and TrieNode have to be declared before they are defined as they are used by class Map.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,559

    Re: Newbie to C++ and visual studio encounters syntax errors during compilation.

    In Visual Studio, if you double-click on the error line on the error window, it will take you to the offending line of cide. Have you tried this?

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,700

    Re: Newbie to C++ and visual studio encounters syntax errors during compilation.

    As the code uses dynamic memory, you also have run-time issues. class Map needs a destructor to free all the memory allocated by .insert(). It also needs a copy constructor and assignment functions (or else these marked as deleted) - and preferably move constructor and move assignment as well.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  6. #6
    Join Date
    Jul 2017
    Posts
    10

    Re: Newbie to C++ and visual studio encounters syntax errors during compilation.

    Guys thanks for the additional info but for now i will stick in my main problem. It seems that 2kaud was right, is the circular reference. I can understand somehow what it is and why it causing problems but i can't really understand it too . I tried to include every single header file inside my Main.cpp file and the program compiles just fine now. But i can't really understand why. Can someone explain me exactly how the compiler thinks and how i must think to include header files? In which order copy-paste the header files and also compiles each cpp file and how it links all of them?
    Last edited by babaliaris; July 8th, 2017 at 06:20 PM.

  7. #7
    Join Date
    Jul 2017
    Posts
    10

    Re: Newbie to C++ and visual studio encounters syntax errors during compilation.

    2kaud thanks you so much! I figure it out!

    From Main.cpp i'm including trie_node.h
    From trie_node.h <--- map.h
    From map.h <--- map_node.h

    but inside map_node.h i have a reference to a TrieNode instance but i still haven't declare this class yet because it will be declared after all these including stuff. So i changed the map_node.h to this:

    Code:
    #pragma once
    //#include "trie_node.h"
    
    class TrieNode;
    
    class MapNode {
    
    private:
        char ch;
        ::MapNode *next;
        TrieNode *trie_node;
    
    public:
        MapNode();
        char getChar();
        ::MapNode *getNext();
        TrieNode *getTrie();
        void setNext(::MapNode *new_node);
        void setCh(char ch);
        void setTrie(TrieNode *new_node);
    };
    This worked like a charm.

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,700

    Re: Newbie to C++ and visual studio encounters syntax errors during compilation.

    Note that declaring a class for use in another class before it is defined only works when it is used as a pointer in that class. It doesn't work if a variable of the declared class is used. This is because c++ needs to know at compile time the size of the class in which the declared class is used. As the size of all pointers are known at compile time, the size of a pointer to an unknown declared class is known at compile time whereas the size of the declared class itself is not. So
    Code:
    TrieNode *trie_node;
    is fine as trie_node is a pointer. But
    Code:
    TrieNode trie;
    is not.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  9. #9
    Join Date
    Jul 2017
    Posts
    10

    Re: Newbie to C++ and visual studio encounters syntax errors during compilation.

    Quote Originally Posted by 2kaud View Post
    Note that declaring a class for use in another class before it is defined only works when it is used as a pointer in that class. It doesn't work if a variable of the declared class is used. This is because c++ needs to know at compile time the size of the class in which the declared class is used. As the size of all pointers are known at compile time, the size of a pointer to an unknown declared class is known at compile time whereas the size of the declared class itself is not. So
    Code:
    TrieNode *trie_node;
    is fine as trie_node is a pointer. But
    Code:
    TrieNode trie;
    is not.
    So for these kind of cases the best way to do things is to include header files in a way that all of them will already been declared before my code use them?

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,700

    Re: Newbie to C++ and visual studio encounters syntax errors during compilation.

    So for these kind of cases the best way to do things is to include header files in a way that all of them will already been declared before my code use them
    Yes. But if that can't be done then you have to use pointers. Consider
    Code:
    struct B;
    
    struct A
    {
    	B bb;
    };
    
    struct B
    {
    	A aa;
    };
    This won't compile as the compiler complains about bb in struct A. This type of circular code can't be made to compile. Only if pointers are used can it. Consider
    Code:
    struct B;
    
    struct A
    {
    	B* bb;
    };
    
    struct B
    {
    	A aa;
    };
    This now compiles.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  11. #11
    Join Date
    Jul 2017
    Posts
    10

    Re: Newbie to C++ and visual studio encounters syntax errors during compilation.

    Thank you very much! I understand everything now. I finished my small test project (Visual Studio Project): Download

    I still have much to learn but i think i did a huge step today. The program does not implement deconstructors but for this purpose it doesn't need any because after exiting the program everything will be delete automatically.

    I'm learning c++ only and only because i want to create VERY fast data structures for a project i'm planning to do in the future (game engines stuff ). Currently i'm creating a 2d game engine in python but some of my data structures aren't fast enough even with log complexity but i'm sure c++ will save me.
    Last edited by babaliaris; July 9th, 2017 at 08:23 AM.

  12. #12
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,700

    Re: Newbie to C++ and visual studio encounters syntax errors during compilation.

    The program does not implement deconstructors but for this purpose it doesn't need any because after exiting the program everything will be delete automatically.
    When dynamic memory allocation is used, it's always a good idea to provide an appropriate destructor. Also a copy constructor and copy assignment. Without providing the proper copies, the compiler will use the default ones which do a shallow copy rather the needed deep copy with dynamic memory usage.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  13. #13
    Join Date
    Jul 2017
    Posts
    10

    Re: Newbie to C++ and visual studio encounters syntax errors during compilation.

    I never heard about copy constructors. Assignment constructors you mean the simple constructors?

    Actually, by copy constructor you mean the clone method?
    In java we call them clone methods
    Last edited by babaliaris; July 9th, 2017 at 09:44 AM.

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,700

    Re: Newbie to C++ and visual studio encounters syntax errors during compilation.

    When dynamic memory is used, specific constructors and assignment operators need to be specified that do deep copying. If dynamic memory is not used then the default ones will probably be OK (which do shallow copying).

    copy constructor for class A allows a correct copy to be made for a new class instance.
    Code:
    A::A(const A& rhs)
    {
        //...
    }
    Assignment operator for class A allows one instance of the class to be copied to another existing instance.
    Code:
    A& A::operator=(A rhs)
    {
       //...
    }
    and the move constructor
    Code:
    A::A(A&& rhs)
    {
        //...
    }
    and the move assignment
    Code:
    A& A::operator=(A&& rhs)
    {
        //...
    }

    These lead to so called rule of three (no move semantics) or rule of five (including rule semantics)

    For more info see
    https://stackoverflow.com/questions/...-five-with-c11
    https://en.wikipedia.org/wiki/Rule_o...B_programming)
    http://www.learncpp.com/cpp-tutorial...y-constructor/
    http://www.learncpp.com/cpp-tutorial...ment-operator/

    For info on move semantics see
    http://www.learncpp.com/cpp-tutorial...ove-semantics/
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)