LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-23-2018, 10:32 PM   #1
xucaen
Member
 
Registered: Feb 2004
Location: Boston
Distribution: Kubuntu 22.04
Posts: 101

Rep: Reputation: 16
class name not found in same namespace?


I have a weird situation which I have not run into before today. Before today I primarily work in windows with Visual Studio, so something may have changed in the c++ standard that I don't under stand (and I don't even know what to search for). The code below is an example of what I am seeing. My code compiles in Visual Studio but not gcc.


I have two classes:

class_a.h
Code:
namespace AAA
{
    namespace BBB
    {
        class MyTest
        {
             
        }
    }
}
class_b.h
Code:
#include "class_a.h"
namespace AAA
{
    namespace BBB
    {
        class AnotherClass
        {
             virtual MyTest getMyTest() = 0;//error: ‘MyTest’ has not been declared

        }
    }
}
In this example, I get a compiler error "‘MyTest’ has not been declared".
I can't see what I am doing wrong. It's probably a compiler parameter I need, but then I don't even know what this is.. is this some kind of scope issue? or are nested namespaces only allowed under certain circumstances? Or maybe it has something to do with virtual? I have no idea. any help is appreciated.
 
Old 02-23-2018, 10:41 PM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
this is what I am getting.
Code:
$ g++ main.cpp
In file included from class_b.h:1:0,
                 from main.cpp:3:
class_a.h:5:15: error: redefinition of 'class AAA::BBB::MyTest'
         class MyTest
               ^~~~~~
In file included from main.cpp:2:0:
class_a.h:5:15: note: previous definition of 'class AAA::BBB::MyTest'
         class MyTest
               ^~~~~~
main
Code:
#include <iostream>
#include "class_a.h"
#include "class_b.h"

int main() {

	
}
with direct copies of your code with added ; at end of class declaration.
 
Old 02-24-2018, 01:13 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Seemed to work without error for me and all I changed was adding a return in main and not including class_a.h twice.
So main now looked like:
Code:
#include <iostream>
#include "class_b.h"

int main() {

	return 0;
	
}
All other files as is and compiled using same command as BW-userx.

Which also begs the question on what was missed in original question, how did you try to compile your code?
 
Old 02-24-2018, 08:05 AM   #4
xucaen
Member
 
Registered: Feb 2004
Location: Boston
Distribution: Kubuntu 22.04
Posts: 101

Original Poster
Rep: Reputation: 16
I apologize to everyone reading this. I know better than to post a technical question late at night without including all the technical details, and yet I did it anyway. I am working on an accurate example now to recreate the issue. I will post more later.
 
Old 02-24-2018, 08:10 AM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by xucaen View Post
I apologize to everyone reading this. I know better than to post a technical question late at night without including all the technical details, and yet I did it anyway. I am working on an accurate example now to recreate the issue. I will post more later.
if you cannot (re)create it then why create it. if it is gone then let it be gone and move on with it.that's what I say. no harm no foul.
 
Old 02-24-2018, 08:43 AM   #6
xucaen
Member
 
Registered: Feb 2004
Location: Boston
Distribution: Kubuntu 22.04
Posts: 101

Original Poster
Rep: Reputation: 16
OK, here is an accurate example that recreates my issue exactly. I have a typedef which has no semicolon. I'm not sure when they started requiring that typedefs have semicolons but I have never used semicolons with typedefs before, and my original code compiles in Visual Studio (I did not specifically compile this example in Visual Studio, I only mean to say that I have similar source code that has typedefs with no semicolons that does compile in VS)

So, is there a way to tell g++ to allow typedefs with no semicolons? For now, I'll just put in the semicolon and be done with it.

MyTest.h
Code:
#pragma once

typedef int CCCC

namespace AAAA
{
	namespace BBBB 
	{
		class MyTest
		{
		public:
			void test();
		};

	}
}

MyTest.cpp
Code:
#include "MyTest.h"
namespace AAAA
{
	namespace BBBB 
	{

		void MyTest::test()
		{
		}

	}
}
makefile
Code:
CC = g++
CFLAGS = -g -Wall -ansi  
COBJ = -c
CEXE = -o $@
SRC=MyTest.cpp
HDR=MyTest.h  

all:	mytest.o

mytest.o: $(SRC) $(HDR)
	$(CC) $(CFLAGS) $(COBJ) $(SRC)

run make.

Compiler output with typedef:

Quote:
g++ -g -Wall -ansi -c MyTest.cpp
In file included from MyTest.cpp:1:0:
MyTest.h:5:1: error: expected initializer before ‘namespace’
namespace AAAA
^~~~~~~~~
MyTest.cpp:7:8: error: ‘MyTest’ has not been declared
void MyTest::test()
^~~~~~
makefile:11: recipe for target 'mytest.o' failed
make: *** [mytest.o] Error 1
Compiler output with no typedef:

Quote:
g++ -g -Wall -ansi -c MyTest.cpp
 
Old 02-24-2018, 09:09 AM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
adding that semi colon
Code:
$ make
g++ -g -Wall -ansi   -c MyTest.cpp
userx@slackwhere101:~/testdir
$ ls
Makefile  MyTest.cpp  MyTest.h  MyTest.o
I'd think that you would/might have to tell in your makefile what version specs of c++ to use to have it use the rules that you stated did not require a semi colon at the end of a typedef like but not necessarily this one
Code:
-std=c++11
 
Old 02-24-2018, 09:19 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
I think most of the characters in the source code are important, there no is point leaving out some of them.
 
Old 02-24-2018, 11:50 AM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by xucaen View Post
I have a typedef which has no semicolon. I'm not sure when they started requiring that typedefs have semicolons but I have never used semicolons with typedefs before, and my original code compiles in Visual Studio (I did not specifically compile this example in Visual Studio, I only mean to say that I have similar source code that has typedefs with no semicolons that does compile in VS)
AFAIK, C (and C++) always required a semicolon at the end of a typedef statement. Perhaps you're confusing it with a "fake typedef" via the proprocessor? E.g.

Code:
#define CCCC int
// almost the same as
typedef int CCCC;
 
Old 02-25-2018, 09:22 AM   #10
xucaen
Member
 
Registered: Feb 2004
Location: Boston
Distribution: Kubuntu 22.04
Posts: 101

Original Poster
Rep: Reputation: 16
No, I copied the code from my Windows PC. The code builds and runs perfectly well in Windows 10 without the semicolon on the typedef. Give it a try in visual studio.
 
Old 02-25-2018, 09:29 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
that is a question: if it works with ; on linux. I do not really care about Visual Studio. Different compiler - different syntax checker. By the way which version of gcc/g++ did you try?
 
Old 02-25-2018, 09:42 AM   #12
xucaen
Member
 
Registered: Feb 2004
Location: Boston
Distribution: Kubuntu 22.04
Posts: 101

Original Poster
Rep: Reputation: 16
I'm using whatever comes stock with Ubuntu 17.1.
 
Old 02-25-2018, 09:51 AM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
gcc --version
 
Old 02-25-2018, 02:15 PM   #14
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by xucaen View Post
No, I copied the code from my Windows PC. The code builds and runs perfectly well in Windows 10 without the semicolon on the typedef. Give it a try in visual studio.
yeahhhh do you see what is going on here? Windows is causing you to develop a habit of not being compatible with anything other than Windows habits. try it the way it works in Linux in your visual code whatever thingy and see if it accepts the typedef with a semicolon.
 
1 members found this post helpful.
Old 02-25-2018, 02:27 PM   #15
xucaen
Member
 
Registered: Feb 2004
Location: Boston
Distribution: Kubuntu 22.04
Posts: 101

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by BW-userx View Post
yeahhhh do you see what is going on here? Windows is causing you to develop a habit of not being compatible with anything other than Windows habits. try it the way it works in Linux in your visual code whatever thingy and see if it accepts the typedef with a semicolon.

That's what I said to my wife when I realized what the problem was. Lol. Microsift makes programmers lazy.
 
  


Reply

Tags
c++



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] [C++] How to access a private member of a class through a friend function of another namespace? Aquarius_Girl Programming 5 02-12-2013 05:14 AM
The type or namespace 'QuitEventArgs' could not be found. tw1tch Programming 1 01-19-2013 08:25 PM
[SOLVED] Dovecot autocreate plugin - no namespace found GDarkBladE Linux - Server 1 05-25-2011 04:32 PM
friend class doesn't work when I put in namespace? Winter Knight Programming 10 12-13-2007 12:57 PM
C++ Vector Class without Namespace sadarax Programming 6 02-02-2006 05:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration