My code :
main.cpp
#include <iostream>
namespace file {
#include "file.cpp"
}
namespace file2 {
#include "file1.cpp"
}
int main() {
file::hello();
return 0;
}
file.cpp
#include <iostream>
void hello() {
std::cout << "hello";
}
file1.cpp
#include <iostream>
void hello() {
std::cout << "hello world";
}
My problem:
I use virtual studio, and i don't know why this does not work.I try it allready on CodeBlock and it was fine. But in VS i have error with at least one repeatedly defined symbol has been found.
Sorry for my english.
Since VS still builds and links file.cpp and file1.cpp to the executable you get the errors. You can exclude them from the project by changing its properties or make them regular header files and include them as such.
The include will literally just copy the content of the file into the name space declaration of main.cpp.
Related
I've searched for the error LNK2005 "already defined in .obj" but can't find content related to the specific problem I am facing. Hope someone can help me on this...
I've a header foo.h
// foo.h
#ifndef FOO_H
#define FOO_H
void foo() {
print("foo\n");
}
#endif
and main file... main.cpp
// main.cpp
#include <thread>
#include "foo.h"
int main() {
std::thread t(foo);
t.join();
return 0;
}
Now, it compile without any errors and gives the gives output to the console...
foo
But if I create another file foo.cpp and just include the header foo.h and do nothing else...
// foo.cpp
#include "foo.h"
...I get linker error LNK2005 "void __cdecl foo(void)" (?foo##YAXXZ) already defined in main.obj
Don't know what's going wrong here.?!!
You must place only the prototype of the foo() function in the header file, and the implementation once in the .cpp.
Thus, foo.h must contain:
#pragma once
void foo();
And foo.cpp:
#include "foo.h"
void foo() {
printf("Whatever");
}
Help, I'm using VC++ and I always get the LNK2005 and LNK1169 Error when running my script, can you guys please tell me why it's happening and how to fix it, Thank you!
Code:
In the Main.cpp
#include "stdafx.h
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "Modifier.h"
using namespace std;
HWND myconsole = GetConsoleWindow();
HDC mydc = GetDC(myconsole);
int main()
{
if (Input.beg("Hello"))
{
cout << "World";
}
cin.ignore();
}
In "Modifier.cpp"
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct {
bool beg(string a)
{
string b;
getline(cin, b);
if (b == a)
{
return true;
}
else
{
}
}
} Input;
In "Modifier.h"
#include "Modifier.cpp"
You must change your header declaration: you're inadvertently declaring a different, global "Input" variable in every .cpp that includes "Modifier.h".
SUGGESTION:
// Modifier.h
#ifndef MODIFER_H
#define MODIFIER_H
struct Input {
bool beg(string a)
{
string b;
getline(cin, b);
if (b == a)
{
return true;
}
else
{
}
}
};
#endif
Then, in Modifier.cpp:
#include "Modifier.h"
struct Input globalInput;
You should not include a .cpp in a .h. You should include headers in source files, not vice versa.
And you should definitely consider using a "class" instead of a "struct". Because, frankly, that's what your "Input" is: just a method, no state/no data.
How msvc11 can compile printf("msvc"); with only <iostream> header?
#include <iostream>
using namespace std;
int main()
{
printf("test123");
cin.get();
return EXIT_SUCCESS;
}
No error...
msvc11
For Visual Studio 2013, iostream includes istream which includes ostream which includes ios which includes xlocnum which includes cstdio which includes stdio.h. It's just standard header file spill-over.
in resource.h
#define String1 333
in resource.rc
#include <windows.h>
#include "resource.h"
STRINGTABLE
{
STRING1 "hie people"
}
in main.cpp
#include<iostream.h>
#include<resource.h>
#include<windows.h>
using namespace std;
int main{
cout<<here i want to output string value from resource how to call the string;
}
and one more problem i am compiling in code blocks .it says resource.h is not there where i am wrong
I assume that it is Visual C++ and you are using MFC. It is as simple as calling:
::LoadString(...)
And if you are using MFC, then
CString str;
str.LoadString(STRING1)
LoadString from MSDN
An Example here of how to use LoadString
I ran into troubles trying to create a gnome-panel applet with gtkmm. I dealt with most of them, but I'm now kind of blocked.
Quick summary : I tried libpanelappletmm, but every program (even the examples supplied in the source code) segfaults when I try to add the applet in my panel.
So I now use the C library (libpanel-applet). First I looked for a way to wrap the PanelApplet Gobject in a gtkmm C++-object, for example a Gtk::EventBox (PanelApplet inherits from GtkEventBox). I tried to cast it, but Glibmm kept throwing a warning ("Failed to wrap object 'PanelApplet'").
So I created a class "Info", inheriting from Gtk::HBox. In my main.cpp file I declare an instance of it, get the underlying GTK object (gobj method), and use the GTK+ functions to add it into the PanelApplet.
Here's my main.cpp.
#include <iostream>
#include <gtkmm.h>
#include <panel-applet.h>
#include "Info.hpp"
static void manage_timeboxes(BonoboUIComponent *uic, void *applet, const char* data) {
std::cout << "manage" << std::endl;
}
static gboolean getApplet(PanelApplet *applet, const gchar *iid, gpointer data) {
/*
if(iid != "OAFIID:TimeboxingApplet")
return false;
*/
Glib::init();
Gtk::Widget* content = new Info();
gtk_container_add(GTK_CONTAINER(applet), content->gobj());
static const char menu_xml[] =
"<popup name=\"button3\">\n"
" <menuitem name=\"Manage\" "
" verb=\"manage_timeboxes\" "
" _label=\"_GĂ©rer l'emploi du temps\"\n"
" pixtype=\"stock\" "
" pixname=\"gtk-properties\"/>\n"
"</popup>\n";
static const BonoboUIVerb linked_verbs[] = {
BONOBO_UI_VERB ("manage_timeboxes", manage_timeboxes),
BONOBO_UI_VERB_END
};
panel_applet_setup_menu(applet, menu_xml, linked_verbs, data);
gtk_widget_show_all(GTK_WIDGET(applet));
return true;
}
PANEL_APPLET_BONOBO_FACTORY (
"OAFIID:TimeboxingApplet_Factory",
PANEL_TYPE_APPLET,
"Timeboxing",
"0.0",
getApplet,
NULL)
It works fine if I add labels or buttons in my Info object.
But then I tried to add an icon.
My first try was adding a Gtk::Image as a property of Info.
Info.hpp
#ifndef TIMEBOXING_INFO_H
#define TIMEBOXING_INFO_H
#include <gtkmm/box.h>
#include <gtkmm/image.h>
#include <gtkmm/label.h>
class Info : public Gtk::HBox {
public:
Info();
virtual ~Info(){};
protected:
Gtk::Image icon;
Gtk::Label info;
};
#endif
Info.cpp
#include "Info.hpp"
#include <gtkmm/image.h>
#include <gtkmm/label.h>
Info::Info() : icon("/home/bastien/programmation/timeboxing-applet/icons/clock-24.png"), info("<b>En cours</b>") {
info.set_use_markup();
pack_start(icon);
pack_start(info);
show_all_children();
}
When I try to add the applet, I get this error and the program aborts :
glibmm:ERROR:objectbase.cc:78:void Glib::ObjectBase::initialize(GObject*): assertion failed: (gobject_ == castitem)
I commented "Gtk::Image icon" from Info.hpp, and I modified my constructor like this :
Info::Info() : info("<b>En cours</b>") {
info.set_use_markup();
Gtk::Image icon("/home/bastien/programmation/timeboxing-applet/icons/clock-24.png");
pack_start(icon);
pack_start(info);
show_all_children();
}
I'm not getting the Glibmm error anymore, but the image isn't displayed. I tried with another file, with an icon from the stock, and even with a Gdk::Pixbuf.
Thank you in advance !
Well, strangely enough, it works if I create a pointer to Gtk::Image.
If anyone has an explanation, it would be great !
Edit : apparently, I had to call Gtk::Main::init_gtkmm_internals. My wrapping troubles went away. I can wrap PanelApplet too, but if I use the resulting Gtk::EventBox* it doesn't display anything.