How to use of FLTK in-box.get_string() - c++

The following code is a small part of an app. For that app I need to use of in_box.get_string(). The declaration and definition of in_box are in this address in GUI.h and GUI.cpp files.
For integers, it works fine. That is, if I use in_box.get_int() it gets the number entered to the in_box and returns that number as an integer. But the problem is that, it doesn't work for strings, as is expected. And when using strings, I get errors!
For example in code below I have used a string s.
#include <GUI.h>
#include <iostream>
using namespace Graph_lib;
class Calculator : public Window {
public:
Calculator(Point, int, int, const string&);
private:
//Widgets
Button show_button;
In_box enter_box;
void show() { getstr(); cout <<s; }
void getstr() {
s = enter_box.get_string();
}
static void cb_show(Address, Address pw) { reference_to<Calculator>(pw).show(); }
string s;
};
//---------------------------------------------------------------------
Calculator::Calculator(Point xy, int w, int h, const string& title):
Window(xy, w, h, title),
show_button(Point(x_max()-110, 440), 90, 35, "Show", cb_show),
enter_box(Point(x_max()-400, 158), 245, 40, "Enter"){
attach(enter_box);
attach(show_button);
}
int main()
{
Calculator cal(Point(100,100), 600, 500, "Calculator");
return gui_main();
}
There are 8 warnings and two errors. Errors are these:
Error 9 error LNK2019: unresolved external symbol "public: class std::basic_string,class
std::allocator > __thiscall Graph_lib::In_box::get_string(void)"
(?get_string#In_box#Graph_lib##QAE?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##XZ)
referenced in function "private: void __thiscall
Calculator::getstr(void)"
(?getstr#Calculator##AAEXXZ) C:\Users\ME\documents\visual studio
2012\Projects\test_3\test_3\test_3.obj Error 10 error LNK1120: 1
unresolved externals C:\Users\ME\documents\visual studio
2012\Projects\test_3\Debug\test_3.exe
The errors say, that is a linker problem but my linker input's file list is as follows and it has worked (almost) fine so far.
fltkd.lib
wsock32.lib
comctl32.lib
fltkjpegd.lib
fltkimagesd.lib
My compiler is MS VS 2012. And machine is Win 7.
Does anyone know how to solve the problem please?

If you have compiled FLTK in Debug and Release mode, make sure you have the linker dependencies right: the files you list for Debug, and fltk.lib, wsock32.lib, comctl32.lib, fltkjpegd.lib and fltkimages.lib for Release (notice absence of "d" at the end of the names).
Secondly, even though I don't think it causes the linker error (but leads to other weird problems), make sure you've included all the Stroustrup graphic library files in your Visual Studio project: fltk.h, Graph.h, GUI.h, Point.h, Simple_window.h, std_lib_facilities.h, Window.h, Graph.cpp, GUI.cpp and Window.cpp.

Related

Error in VS2019 with Google Test when using outside definition

I just started a small console app in VS2019 and added a Google Test project. The program itself compiles and runs fine, however, if I move function code from the header to the cpp-file, I get the LNK2019 and LNK1120 errors:
Severity
Code
Description
Project
File
Line
Error
LNK2019
unresolved external symbol "public: void __cdecl MyClass::someFunction(void)" (?someFunction#MyClass##QEAAXXZ) referenced in function "private: virtual void __cdecl MyTests_basicTest_Test::TestBody(void)" (?TestBody#MyTests_basicTest_Test##EEAAXXZ)
Tests
Tests.obj
1
Error
LNK1120
1 unresolved externals
Tests
Tests.exe
1
MyClass.h:
#pragma once
class MyClass
{
public:
void someFunction();
};
MyClass.cpp:
#include "MyClass.h"
void MyClass::someFunction() {
// do something
}
Main.cpp:
#include "MyClass.h"
int main()
{
MyClass mc;
mc.someFunction();
}
Tests.cpp:
#include "pch.h"
#include "../TestApp/MyClass.h"
TEST(MyTests, basicTest) {
MyClass mc;
mc.someFunction();
EXPECT_EQ(1, 1);
}
I could just put all of the code in the header file of course, but I don't think that is what I'm supposed to do, so I would greatly appreciate some help on this.
Edit:
I feel like I'm one step closer (though it is only a shot in the dark): I added the directory that contains all the *.obj files under [Tests Project] -> "Properties" -> "Configuration Properties" -> "Linker" -> "Input" -> "Additional Dependencies": D:\Application\x64\Debug
However, now I get the following error:
LNK1104 cannot open file 'D:\Application\x64\Debug.obj'
And indeed, the directory contains all kinds of obj-files but not Debug.obj. :/
Edit 2:
Solved! I followed this link under "To link the tests to the object or library files". Important that you really just enter the base name of the file itself without the ".obj".

C++ Unit Test in Visual Studio 2019

I am having a problem with Visual Studio 2019 CPPUnitTestFramework. I follow the instructions, but every time I get an error. I have looked for tutorials, but anything I get is for Visual Studio 2017 and it does not solve my problem.
I am writing a program that uses OOP in C++ and I want to make unit tests, since it is going to be a considerably long project. The problem that I am having is that the program is not compiling in the test module.
Consider that I have the code such that I have the header file:
//A.h
#pragma once
class A
{
private:
// One parameter.
int a;
public:
// Add function.
int add(int, int);
// Subtract function.
int subtract(int, int);
A();
};
with the proper source file:
// A.cpp
#include "A.h"
int a;
int A::add(int alpha, int beta)
{
return alpha + beta;
}
int A::subtract(int alpha, int beta)
{
return alpha - beta;
}
A::A()
{
a = 4;
}
The structure of the program looks something like this:
To make my Unit Test, I right click on the "Solution 'TestTestUnit'" label and choose new project, look for the unit test, add the unit test and attach the reference, such that I get a file structure such as the one below:
To perform the unit test I write the code:
// TestUnitForTestTestUnit.cpp
#include "pch.h"
#include "CppUnitTest.h"
#include "../TestTestUnit/A.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace TestUnitForTestTestUnit
{
TEST_CLASS(TestUnitForTestTestUnit)
{
public:
TEST_METHOD(TestMethod1)
{
A first;
Assert::AreEqual(first.add(3, 2), 5);
}
};
}
When I try to run a test, the Test Explorer does nothing and throws the message: Aborting test run due to build failures. Please see the build for more details.
I cannot find the mistake here. The program runs perfect, but when instantiating a new "A" object the test fails. I am stuck here, are there any suggestions? What am I doing wrong (besides developing in Windows)?
UPDATE:
I have followed the suggestion to remove the namespace, as suggested by #ChrisMM, so that the test file now reads:
// TestUnitForTestTestUnit.cpp
#include "pch.h"
#include "CppUnitTest.h"
#include "../TestTestUnit/A.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
TEST_CLASS(TestUnitForTestTestUnit)
{
public:
TEST_METHOD(TestMethod1)
{
A first;
Assert::AreEqual(first.add(3, 2), 5);
}
};
such that when I run the Test Explorer gives the same message:
with error message:
1>------ Build started: Project: TestUnitForTestTestUnit, Configuration: Debug Win32 ------
1> Creating library C:\Users\<user>\Desktop\CPlusPlus\TestTestUnit\Debug\TestUnitForTestTestUnit.lib and object C:\Users\<user>\Desktop\CPlusPlus\TestTestUnit\Debug\TestUnitForTestTestUnit.exp
1>TestUnitForTestTestUnit.obj : error LNK2019: unresolved external symbol "public: int __thiscall A::add(int,int)" (?add#A##QAEHHH#Z) referenced in function "public: void __thiscall TestUnitForTestTestUnit::TestMethod1(void)" (?TestMethod1#TestUnitForTestTestUnit##QAEXXZ)
1>TestUnitForTestTestUnit.obj : error LNK2019: unresolved external symbol "public: __thiscall A::A(void)" (??0A##QAE#XZ) referenced in function "public: void __thiscall TestUnitForTestTestUnit::TestMethod1(void)" (?TestMethod1#TestUnitForTestTestUnit##QAEXXZ)
1>C:\Users\<user>\Desktop\CPlusPlus\TestTestUnit\Debug\TestUnitForTestTestUnit.dll : fatal error LNK1120: 2 unresolved externals
1>Done building project "TestUnitForTestTestUnit.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========
Help would be appreciated.
You cannot put a test class inside a namespace. From the documentation
TEST_CLASS must be declared at namespace scope.
I suggest you to check if TestUnitForTestTestUnit has added Additional Dependencies. When I didn’t add it, the same problem as you occurred. After I added it, the program worked fine.
Right click TestUnitForTestTestUnit->Properties->C/C++->Linker->Input->Additional Dependencies-> add ..\TestTestUnit\Debug\*.obj

Errors LNK 2019 and LNK2028 for one function in a class, other works fine

I normally work in c# and am out of my wits for this one . I used Walkthrough: Creating and Using a Dynamic Link Library (C++) to create a Dynamic Link Library.
I have defined two methods as shown below
DeveloperConsoleManager.h
#pragma once
#include "atlstr.h"
#ifdef DEVCONSOLEMANAGER_EXPORTS
#define DEVCONSOLEMANAGER_API __declspec(dllexport)
#else
#define DEVCONSOLEMANAGER_API __declspec(dllimport)
#endif
namespace DeveloperConsoleManager
{
class DeveloperConsoleLogic
{
public:
// Returns a + b
static DEVCONSOLEMANAGER_API double Add(double a, double b);
static DEVCONSOLEMANAGER_API bool CheckforValidFile(CString fileName);
};
}
DeveloperConsoleManager.cpp
// DeveloperConsoleManager.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include "DeveloperConsoleManager.h"
namespace DeveloperConsoleManager
{
double DeveloperConsoleLogic::Add(double a, double b)
{
return a + b;
}
bool DeveloperConsoleLogic :: CheckforValidFile(CString fileName)
{
return false;
}
}
I use these methods in a .cpp file in a different project (type: Application (.exe)). When I Build the solution, there are following linker errors
Warning 1 warning C4273: 'DeveloperConsoleManager::DeveloperConsoleLogic::Add' : inconsistent dll linkage e:\md_69\developerconsolemanager\developerconsolemanager.cpp 10
Warning 2 warning C4273: 'DeveloperConsoleManager::DeveloperConsoleLogic::CheckforValidFile' : inconsistent dll linkage e:\md_69\developerconsolemanager\developerconsolemanager.cpp 16
Error 3 error LNK2028: unresolved token (0A0004F1) "public: static bool __cdecl DeveloperConsoleManager::DeveloperConsoleLogic::CheckforValidFile(class ATL::CStringT > >)" (?CheckforValidFile#DeveloperConsoleLogic#DeveloperConsoleManager##$$FSA_NV?$CStringT#_WV?$StrTraitMFC_DLL#_WV?$ChTraitsCRT#_W#ATL#####ATL###Z) referenced in function "public: void __thiscall CSaSsiConsoleUi::UploadSsiCheck(void)" (?UploadSsiCheck#CSaSsiConsoleUi##$$FQAEXXZ) E:\MD_69\DeveloperConsoleUI\SaSsiConsoleUI.obj
Error 4 error LNK2019: unresolved external symbol "public: static bool __cdecl DeveloperConsoleManager::DeveloperConsoleLogic::CheckforValidFile(class ATL::CStringT > >)" (?CheckforValidFile#DeveloperConsoleLogic#DeveloperConsoleManager##$$FSA_NV?$CStringT#_WV?$StrTraitMFC_DLL#_WV?$ChTraitsCRT#_W#ATL#####ATL###Z) referenced in function "public: void __thiscall CSaSsiConsoleUi::UploadSsiCheck(void)" (?UploadSsiCheck#CSaSsiConsoleUi##$$FQAEXXZ) E:\MD_69\DeveloperConsoleUI\SaSsiConsoleUI.obj
Error 5 error LNK1120: 2 unresolved externals E:\MD_69\Debug\DeveloperConsoleUi.exe
There is no linker error for the "Add" method.
I have already included "DeveloperConsoleManager.lib" in Linker -> Input -> Additional Dependencies. Please help me find out what exactly am I doing wrong.
I would be glad to add any additional information needed.
Thanks to #Igor Tandetnik and the awesome thing that is internet, I figured it out. I am adding it as an answer so that some one else might benefit.
The problem was with CString. The project in which the function was defined was a dynamic link library (dll) and the call was being made from an MFC application. Now, the issue was that, MFC uses for CString while the non-MFC dll uses .
CString in is defined as:
typedef ATL::CStringT< TCHAR, StrTraitMFC_DLL< TCHAR > > CString;
while in is defined as:
typedef CStringT< TCHAR, StrTraitATL< TCHAR > > CString;
This, as you can clearly see is different. The workaround I used was using CAtlString instead of CString . However, please feel free to suggest any better way if you come across.

c++ compiling issue -- Compiles fine with cl.exe but will not compile from Visual Studio 2015

******* EDIT *******
One reader suggested that my question was a duplicate of the question found here. The leading response to this question gives two solutions:
1) #include the implementation file at the end of the header (which is what I did in my solution) or
2) explicitly instantiate all of the template instances I'll need at the end of the implementation file
Since I had already attempted this solution, this is not a duplicate question.
The problem, as addressed by Ben Voigt below (thank you), has to do with Visual Studio. Changing the "Item Type" from "C/C++ compiler" to "C/C++ header" for the MyClass.cpp file resolved the issue.
**** END EDIT ****
I'm a beginner to c++ and I'm running into an issue trying to compile the following program in Visual Studio 2015. The program compiled fine using the clang compiler on my Mac. The program also compiled fine using cl.exe from the Developer Command Prompt for VS2015. I guess this means something is configured wrong in my Visual Studio project, but I haven't found a solution.
Here's the code:
/** #file MyInterface.h */
#ifndef MY_INTERFACE_
#define MY_INTERFACE_
template<class ItemType>
class MyInterface
{
public:
virtual void sayHello() const = 0;
};
#endif
/** #file MyClass.h */
#ifndef MY_CLASS_
#define MY_CLASS_
#include "MyInterface.h"
template<class ItemType>
class MyClass : public MyInterface<ItemType>
{
private:
ItemType myItem;
public:
MyClass();
void setItem(const ItemType& newItem);
void sayHello() const;
};
#include "MyClass.cpp"
#endif
/** Implementation file for MyClass.
#file MyClass.cpp */
#include "MyClass.h"
#include <iostream>
template<class ItemType>
MyClass<ItemType>::MyClass()
{
}
template<class ItemType>
void MyClass<ItemType>::setItem(const ItemType& newItem)
{
myItem = newItem;
}
template<class ItemType>
void MyClass<ItemType>::sayHello() const
{
std::cout << "Hello! My Item is: " << myItem << std::endl;
}
/** #file test.cpp */
#include "MyClass.h"
#include <string>
int main()
{
MyClass<std::string> classOne;
classOne.setItem("foo");
classOne.sayHello();
MyClass<int> classTwo;
classTwo.setItem(7);
classTwo.sayHello();
}
When I combine these files in one directory and run the command:
cl test.cpp
from the Developer Command Prompt for VS2015, it correctly compiles. I get a test.exe executable file which returns:
test.exe
Hello! My item is: foo
Hello! My item is: 7
So far, so good.
The problem begins when I try to complete this project inside of Visual Studio 2015. I started an "Empty Project" for Visual c++ and added these exact same files. However, when I build the project I get the error messages:
Error C2995 'MyClass<ItemType>::MyClass(void)': function template has already been defined
Error C2995 'void MyClass<ItemType>::setItem(const ItemType &)': function template has already been defined
Error C2995 'void MyClass<ItemType>::sayHello(void) const': function template has already been defined
The error message leads me to believe that I have a circular dependency. The culprit appears to be the #include "MyClass.cpp" in the MyClass.h file, but this is necessary for the template to compile. Without this inclusion, the compiler cannot see the instantiation of the template and does not know the actual data type corresponding to the generic type ItemType (or so my textbook says).
In an effort to satisfy the Visual Studio build process, I removed the #include "MyClass.cpp from the MyClass.h file, but then I get THESE errors:
Error LNK2019 unresolved external symbol "public: __thiscall MyClass<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::MyClass<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(void)" (??0?$MyClass#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std####QAE#XZ) referenced in function _main
Error LNK2019 unresolved external symbol "public: __thiscall MyClass<int>::MyClass<int>(void)" (??0?$MyClass#H##QAE#XZ) referenced in function _main
Error LNK2019 unresolved external symbol "public: void __thiscall MyClass<int>::setItem(int const &)" (?setItem#?$MyClass#H##QAEXABH#Z) referenced in function _main
There were six such errors (only three are shown), all being unresolved external symbols.
At this point I don't know what else to try. If I include the implementation file in the header, the compiler yells at me for having circular dependencies, and if I DON'T include the implementation file in the header, I have unresolved external symbols.
Any help on this problem would be much appreciated :)
Because you #include MyClass.cpp, it isn't a compilation unit itself, it's a header file. Make sure Visual Studio is told not to try to build it separately, by default it assumes .h files are headers and .cpp are each individually compiled units.
Access the item properties and change "Item Type" from "C/C++ compiler" to "C/C++ header".
This effectively changes the build command to the working version
cl test.cpp
from the default (effective) command generated by adding all the files
cl test.cpp MyClass.cpp
which would cause exactly the same errors if you typed that at the command line.
There are other ways to accomplish removing it from the build command, like "Exclude file from project", found on the right-click menu in Solution Explorer, but that makes it less convenient to edit/navigate to. Marking it correctly as a header file will give you the best experience for Intellisense auto-completion, Goto Definition, and source view during debugging.

C++ LNK2019 error with constructors and destructors in derived classes

I have two classes, one inherited from the other. When I compile, I get the following errors:
Entity.obj : error LNK2019: unresolved external symbol "public: __thiscall Utility::Parsables::Base::Base(void)" (??0Base#Parsables#Utility##QAE#XZ) referenced in function "public: __thiscall Utility::Parsables::Entity::Entity(void)" (??0Entity#Parsables#Utility##QAE#XZ)
Entity.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall Utility::Parsables::Base::~Base(void)" (??1Base#Parsables#Utility##UAE#XZ) referenced in function "public: virtual __thiscall Utility::Parsables::Entity::~Entity(void)" (??1Entity#Parsables#Utility##UAE#XZ)
D:\Programming\Projects\Caffeine\Debug\Caffeine.exe : fatal error LNK1120: 2 unresolved externals
I really can't figure out what's going on.. can anyone see what I'm doing wrong? I'm using Visual C++ Express 2008. Here are the files..
"include/Utility/Parsables/Base.hpp"
#ifndef CAFFEINE_UTILITY_PARSABLES_BASE_HPP
#define CAFFEINE_UTILITY_PARSABLES_BASE_HPP
namespace Utility
{
namespace Parsables
{
class Base
{
public:
Base( void );
virtual ~Base( void );
};
}
}
#endif //CAFFEINE_UTILITY_PARSABLES_BASE_HPP
"src/Utility/Parsables/Base.cpp"
#include "Utility/Parsables/Base.hpp"
namespace Utility
{
namespace Parsables
{
Base::Base( void )
{
}
Base::~Base( void )
{
}
}
}
"include/Utility/Parsables/Entity.hpp"
#ifndef CAFFEINE_UTILITY_PARSABLES_ENTITY_HPP
#define CAFFEINE_UTILITY_PARSABLES_ENTITY_HPP
#include "Utility/Parsables/Base.hpp"
namespace Utility
{
namespace Parsables
{
class Entity : public Base
{
public:
Entity( void );
virtual ~Entity( void );
};
}
}
#endif //CAFFEINE_UTILITY_PARSABLES_ENTITY_HPP
"src/Utility/Parsables/Entity.cpp"
#include "Utility/Parsables/Entity.hpp"
namespace Utility
{
namespace Parsables
{
Entity::Entity( void )
{
}
Entity::~Entity( void )
{
}
}
}
The relevant bit is this:
unresolved external symbol "public: __thiscall Utility::Parsables::Base::Base(void)"
You need to provide a definition for Base::Base and Base::~Base. A declaration is not good enough. Even if you have nothing to do in either function, you need to leave an empty function body, because C++ actually requires the function to exist. C++ puts things like virtual table maintenance inside your constructors and destructors, so they must be defined even if you don't need to do anything there -- C++ has to do things in there.
Are you sure Base.cpp is being included in the build?
Just encountered this exact same error today in Visual Studio 2015. Unfortunately the accepted answer didn't worked (as well as answers from many same questions). The thing that worked for me was right click on the base class cpp file, exclude and then include it again. I think somehow VS got confused while moving file around and renames and it just silently refused to compile it even though it was marked as "Included In project" = true in property editor as well as listed in vcproj file in group. This is horrible error and ended up spending good hour on it.
Either your base.cpp is not being compiled/linked or you have a misspelling in it