ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
#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.
$ 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.
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.
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.
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();
};
}
}
$ 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
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;
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.
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?
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.