[QUOTE=Mega1mpact;41827105][url]https://github.com/Mega1mpact/VisualSort/[/url]
I hope that this will help you to understand how I did the visual sorting.
I'll have a look at your code once my visual studio finishes installing[/QUOTE]
Thanks a lot! :)
[QUOTE=ichiman94;41827637]
You really sorted them, but you forgot about their position, so they stay at where they were created. Just set their position too and it will work. You can even swap their scales too, so you don't have to reposition them.[/QUOTE]
Damn! I can't believe I missed something like that! I'm on the way to work now so I'll post when I get back if I have any progress.
Anyone see what i'm missing here? (It's probably something stupid since i wrote this code last night and went to bed before i could test, and now i noticed that it doesn't work :v
TestState.cs:
[code]
public class GuiTestState : GameState {
private SpriteBatch _spriteBatch;
private GuiCanvas _canvas;
private GuiWindow _window;
public override void LoadContent(Microsoft.Xna.Framework.Content.ContentManager content) {
_spriteBatch = new SpriteBatch(KuubGame.Graphics.GraphicsDevice);
_canvas = new GuiCanvas();
_window = new GuiWindow(_canvas);
_window.Bounds = new Rectangle(32, 32, 256, 256);
_window.Caption = "Hello World!";
base.LoadContent(content);
}
public override void Update(GameTime gameTime) {
_canvas.Update(gameTime);
base.Update(gameTime);
}
public override void Draw(GameTime gameTime) {
_spriteBatch.Begin();
_canvas.Draw(gameTime, _spriteBatch);
_spriteBatch.End();
base.Draw(gameTime);
}
}
[/code]
GuiBase.cs:
[code]
public abstract class GuiBase {
public string Name { get; set; }
private GuiBase _parent;
public GuiBase Parent {
get { return _parent; }
set {
if (_parent == value) return;
if (_parent != null)
_parent.RemoveChild(this);
_parent = value;
if (_parent != null)
_parent.AddChild(this);
}
}
public GuiBase InnerPanel { get; set; }
private List<GuiBase> _children;
public List<GuiBase> Children {
get { return InnerPanel != null ? InnerPanel.Children : _children; }
}
private Rectangle _bounds;
public Rectangle Bounds {
get { return _bounds; }
set { _bounds = value; }
}
public int X {
get { return Bounds.X; }
set { _bounds.X = value; }
}
public int Y {
get { return Bounds.Y; }
set { _bounds.Y = value; }
}
public Color FontColor { get; set; }
public Color BackgroundColor { get; set; }
public Margin Margin { get; set; }
public Padding Padding { get; set; }
public Vector2 MinSize { get; set; }
public Vector2 MaxSize { get; set; }
protected GuiBase(GuiBase parent) {
_children = new List<GuiBase>();
Parent = parent;
Padding = Padding.Zero;
Margin = Margin.Zero;
FontColor = Color.White;
BackgroundColor = Color.Black;
MinSize = Vector2.One;
MaxSize = new Vector2(4096);
_bounds = new Rectangle(0, 0, (int)MinSize.X, (int)MinSize.Y);
}
public virtual void AddChild(GuiBase child) {
if (InnerPanel != null) {
InnerPanel.AddChild(child);
} else {
//child.Parent = this;
Children.Add(child);
}
OnChildAdded(child);
}
protected virtual void OnChildAdded(GuiBase child) {}
public virtual void Update(GameTime gameTime) {
foreach (GuiBase child in Children)
child.Update(gameTime);
}
public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch) {
foreach (GuiBase child in Children)
child.Draw(gameTime, spriteBatch);
}
}
[/code]
GuiWindow.cs
[code]
public class GuiWindow : GuiPanel {
private GuiLabel _title;
private GuiPanel _panel;
public string Caption {
get { return _title.Text; }
set { _title.Text = value; }
}
public GuiWindow(GuiBase parent) : base(parent) {
Name = "Window";
_title = new GuiLabel(this);
_panel = new GuiPanel(this);
//InnerPanel = _panel;
}
}
[/code]
GuiPanel.cs
[code]
public class GuiPanel : GuiBase {
public GuiPanel(GuiBase parent) : base(parent) {
Name = "Panel";
}
public override void Draw(GameTime gameTime, SpriteBatch spriteBatch) {
base.Draw(gameTime, spriteBatch);
System.Console.WriteLine("drawing panel " + Name + " @ " + Bounds);
spriteBatch.Draw(DrawingHelper.WhitePixel, Bounds.Move(Parent.X + Parent.Padding.Left, Parent.Y + Parent.Padding.Top), BackgroundColor);
}
}
[/code]
GuiLabel.cs
[code]
public class GuiLabel : GuiBase {
public string Text { get; set; }
public GuiLabel(GuiBase parent) : base(parent) {
Name = "Label";
}
}
[/code]
Console constantly outputs this, and i have no idea why it's saying window
[code]
drawing panel Panel @ {X:0 Y:0 Width:1 Height:1}
drawing panel Window @ {X:32 Y:32 Width:256 Height:256}
drawing panel Panel @ {X:0 Y:0 Width:1 Height:1}
drawing panel Window @ {X:32 Y:32 Width:256 Height:256}
[/code]
[QUOTE=Goz3rr;41831851]Anyone see what i'm missing here? (It's probably something stupid since i wrote this code last night and went to bed before i could test, and now i noticed that it doesn't work :v
[/QUOTE]
Quick question:
Is this a typo?
[code] public class GuiTestState class="operator">: GameState {[/code]
[QUOTE=Mega1mpact;41832972]Quick question:
Is this a typo?
[code] public class GuiTestState class="operator">: GameState {[/code][/QUOTE]
The text in the [code] tags is actually "public class GuiTestState : GameState {" but i guess garry broke something with them
[QUOTE=Goz3rr;41831851]Anyone see what i'm missing here? (It's probably something stupid since i wrote this code last night and went to bed before i could test, and now i noticed that it doesn't work :v
[code]//code[/code]
Console constantly outputs this, and i have no idea why it's saying window
[code]
drawing panel Panel @ {X:0 Y:0 Width:1 Height:1}
drawing panel Window @ {X:32 Y:32 Width:256 Height:256}
drawing panel Panel @ {X:0 Y:0 Width:1 Height:1}
drawing panel Window @ {X:32 Y:32 Width:256 Height:256}
[/code][/QUOTE]
It's saying Window because you have Panel outputting its Name-property, GuiWindow inherits from GuiPanel and sets "Window" as its Name.
I currently have a class for graphics, which basically holds all the graphics info together. I also have other data classes like model and player, and I also have a helper class (well really just functions) that load shaders and that sort of stuff. In my graphics class I define GLFW_DLL and _USE_MATH_DEFINES. It also includes GLM, GLEW, and GLFW. Now, is it a good or bad idea to remove the dependency of these classes from graphics? That is, move the GLFW_DLL define into the file with main(), and define _USE_MATH_DEFINES in whatever header needs it? On top of this, include GLM, GLEW, and GLFW whenever they are needed.
[QUOTE=WTF Nuke;41840436]In my graphics class I define GLFW_DLL and _USE_MATH_DEFINES. It also includes GLM, GLEW, and GLFW. Now, is it a good or bad idea to remove the dependency of these classes from graphics?[/QUOTE]
Either you have the dependency, or you don't.
If you don't, don't include the headers.
If you do and you meant you want to refactor somehow and hide the dependency with wrapper classes, then it depends on how likely you think you will want to switch out a library. This could also help with API-incompatible releases of a library though, since the changes you have to make are centralized in one section of the code.
[QUOTE]That is, move the GLFW_DLL define into the file with main(), and define _USE_MATH_DEFINES in whatever header needs it?[/QUOTE]
If the header-files use the usual header-guards and you need the GLFW_DLL define to expose some additional functionality, then you should have it before every #include of the header.
Alternatively you can setup the compiler to have the preprocessor macro defined during the whole build (-DGLFW_DLL for gcc or clang).
[QUOTE]On top of this, include GLM, GLEW, and GLFW whenever they are needed.[/QUOTE]
This is in general a good thing to reduce time and memory required during the build.
Thanks Zeeky. On the first point, what I meant to say is that my classes such as model uses GLM. Before, I would have model include "graphics.h", and this is what I was wondering about. The model class will still be dependent on GLM, however I would include graphics.h instead of GLM. I have changed this because it means I don't have to recompile a load of classes whenever graphics changes.
Personally I always include specifically what I use.
For example, fstream might include ostream and istream, but if I use both (fstream and either ostream and/or istream) separately, I still include both.
It's probably a given, but I might change code and at some point not require the fstream in there anymore. I can just remove the #include <fstream> and the compiler will still find std::ostream and std::istream.
Same goes for myheader.hpp. The class I define in there might define a std::vector-member, thus have vector included, but if I use it separately I still include vector in the implementation file.
As far as "collection-headers" go, by which I mean headers with the purpose of including a bunch of other headers, I wouldn't do that unless you have big build-times due to that and can speed it up via such a method.
Also look into pre-compiled headers then, and in the future, [url=http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3347.pdf]modules[/url] ([url=http://isocpp.org/blog/2012/11/modules-update-on-work-in-progress-doug-gregor]talk[/url]) should help.
[QUOTE=ZeekyHBomb;41840271]It's saying Window because you have Panel outputting its Name-property, GuiWindow inherits from GuiPanel and sets "Window" as its Name.[/QUOTE]
Oh damn, that was supposed to inherit GuiBase. Thanks!
I need some help with databinding in c#
I have a class that has some IObservable collections in it. Here is a minimalistic example :
[code]
public class Customer
{
public string CompanyName { get; set; }
public string PostalCode { get; set; }
public string Address { get; set; }
public string Phone { get; set; }
public string City { get; set; }
}
public class Database : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private ObservableCollection<Customer> m_Customers = new ObservableCollection<Customer>();
public ObservableCollection<Customer> Customers
{
get { return m_Customers; }
set { m_Customers = value; OnPropertyChanged("Customers"); }
}
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
[/code]
Now when I add a item to customers it works fine and displays it in a listview. The problem comes when I try to copy a existing database into the database, it just stops displaying it altogether.
Example (I use json to send shit over the network)
[code]
public void SyncData(string a_Data)
{
m_Database = JsonConvert.DeserializeObject<Database>(a_Data);
}
[/code]
I checked m_Database and it seems to populate fine but the data binding just stops. Any idea's ?
Is m_Database.PropertyChanged still correct after you deserialize the Database?
Alright, this access violation is really starting to piss me off. I managed to get rid of it after rewriting my terrain loader code but suddenly today it came back again.
This is the error that I get:
[t]http://i.imgur.com/lsPstRK.png[/t]
and it points me to this piece of code in _file.c:
[code]
void __cdecl _lock_file (
FILE *pf
)
{
/*
* The way the FILE (pointed to by pf) is locked depends on whether
* it is part of _iob[] or not
*/
if ( (pf >= _iob) && (pf <= (&_iob[_IOB_ENTRIES-1])) )
{
/*
* FILE lies in _iob[] so the lock lies in _locktable[].
*/
_lock( _STREAM_LOCKS + (int)(pf - _iob) );
/* We set _IOLOCKED to indicate we locked the stream */
pf->_flag |= _IOLOCKED;
}
else
/*
* Not part of _iob[]. Therefore, *pf is a _FILEX and the
* lock field of the struct is an initialized critical
* section.
*/
EnterCriticalSection( &(((_FILEX *)pf)->lock) );
}
[/code]
and this is the stack trace:
[t]http://i.imgur.com/67kUykM.png[/t]
Now, the weird thing is that if I go into my terrain code and change it to use SOIL to load the heightmap instead of FreeImage, this error doesn't occur.
[code]
int width, height, channels;
BYTE* bDataPointer = SOIL_load_image( Utils::contentPath( sPath ).c_str(), &width, &height, &channels, SOIL_LOAD_RGB );
[/code]
The above code doesn't fail, however the following does:
[code]
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
FIBITMAP* dib(0);
fif = FreeImage_GetFileType( Utils::contentPath( sPath ).c_str(), 0 );
if( fif == FIF_UNKNOWN )
fif = FreeImage_GetFIFFromFilename( Utils::contentPath( sPath ).c_str() );
if( fif == FIF_UNKNOWN )
{
printf( "ERROR! Failed to figure out the filetype of height map." );
return;
}
if( FreeImage_FIFSupportsReading( fif ) )
dib = FreeImage_Load( fif, Utils::contentPath( sPath ).c_str() );
else
{
printf( "ERROR! Failed to load height map." );
return;
}
BYTE* bDataPointer = FreeImage_GetBits(dib);
if( bDataPointer == NULL )
{
printf( "ERROR! HeightMap file could not be loaded. (Did you make sure the path is correct?)\n" );
return;
}
int width = FreeImage_GetWidth( dib ), height = FreeImage_GetHeight( dib );
[/code]
So a seemingly completely unrelated change in my terrain loading code makes FreeType, which is used in my font loading code, to error out. Could anyone please help me out with this somehow? It's an frustrating error to get because it seems so completely random and I've never tried anything like it before.
The error is due to the file, that is intended to be locked, being nullptr.
That changing an unrelated piece of code makes it not crash is curious. It seems like either heap or stack corruption is taking place. I recommend looking for a Windows-equivalent to AddressSanitizer or Valgrind.
[QUOTE=ZeekyHBomb;41862467]The error is due to the file, that is intended to be locked, being nullptr.
That changing an unrelated piece of code makes it not crash is curious. It seems like either heap or stack corruption is taking place. I recommend looking for a Windows-equivalent to AddressSanitizer or Valgrind.[/QUOTE]
I found something called Dr. Memory which seemed like it's equivalent to Valgrind, but I must admit that I've never tried to use something like it before and couldn't really figure out anything from it's report. This is what it says:
[url]http://pastebin.com/Hh5BZSFt[/url] (It's quite long apparently)
I appreciate any help with deciphering that report, lol.
A lot of false positives. You can basically ignore anything which doesn't include your code.
So I just searched for "upengine" and the first relevant error then is at line 483 in that report.
"Error #31: INVALID HEAP ARGUMENT: allocated with operator new, freed with free"
This comes again as error #32 and #33.
Look if that is a problem with your code.
Error #34 is the one that's causing the access violation. There doesn't seem to be any information you didn't already get from the debugger though. Maybe you can fix that when fixing the previous three though.
Yep, I saw those errors but I'm not sure what it's trying to tell me seeing as all three errors supposedly happen within Assimp which I use for my asset loading. The line that all three references is this one:
[code]
const aiScene* pScene = importer.ReadFile( Utils::contentPath(sPath), aiProcess_Triangulate | aiProcess_GenSmoothNormals );
[/code]
which doesn't leave alot of space for error really.
This is the function:
[code]
bool Mesh::loadMesh( const std::string& sPath )
{
clear();
Assimp::Importer importer;
const aiScene* pScene = importer.ReadFile( Utils::contentPath(sPath), aiProcess_Triangulate | aiProcess_GenSmoothNormals );
if( pScene != NULL )
{
glGenVertexArrays( 1, &_uiVAO );
glBindVertexArray( _uiVAO );
initFromScene( pScene, sPath );
glBindVertexArray( NULL );
importer.FreeScene();
}
else
{
printf( "ERROR! Failed to load mesh from %s (Was the path entered correctly?)\n", sPath.c_str() );
return false;
}
return true;
}
[/code]
I tried running Dr. Memory on the code using SOIL instead, and this is the report:
[URL]http://pastebin.com/Uq1AdvQR[/URL]
The interesting thing is that there's none of the other 3 errors here, however there is this error that pops up now that isn't in the other report:
[code]
Error #32: WARNING: heap allocation failed
# 0 MSVCR100D.dll!operator new [f:\dd\vctools\crt_bld\self_x86\crt\src\new.cpp:59]
# 1 operator new[] [f:\dd\vctools\crt_bld\self_x86\crt\src\newaop.cpp:6]
# 2 Terrain::loadHeightMap [c:\users\capsup\documents\visual studio 2010\projects\upengine\upengine\terrain.cpp:51]
# 3 main [c:\users\capsup\documents\visual studio 2010\projects\upengine\upengine\main.cpp:144]
Note: @0:00:16.615 in thread 11252
[/code]
which references this line:
[code]
float* p_fData = new float[ width * height * 3 ];
[/code]
I'm not sure what could be going wrong here either, as width and height are only 128 each, so it's not a very big array. And this array is used for my terrain data which works fine, so I'm not even sure it actually doesn't work.
It seems all three are in the standard library and not your own code.
It seems [url=http://www.gamedev.net/topic/616692-freetype-ft-new-face-crashing/]you're not the first to have this problem[/url].
Since the FILE* is nullptr make doubly and triply sure that the font you're loading exists. Although that still would not explain why it does not crash with the SOIL code.
You can also build FreeType with debug configuration and look at their functions in the callstack when the crash occurs. Maybe you can find something interesting there.
[QUOTE=ZeekyHBomb;41863032]It seems all three are in the standard library and not your own code.
It seems [URL="http://www.gamedev.net/topic/616692-freetype-ft-new-face-crashing/"]you're not the first to have this problem[/URL].
Since the FILE* is nullptr make doubly and triply sure that the font you're loading exists. Although that still would not explain why it does not crash with the SOIL code.
You can also build FreeType with debug configuration and look at their functions in the callstack when the crash occurs. Maybe you can find something interesting there.[/QUOTE]
I'm 100% sure that the font exists, because it works perfectly if I just use the SOIL code instead.
I'll try to look into FreeType with debug configuration, but once again, I've never tried anything like this before so wish me luck! :v:
About the heap allocation failure:
Not sure. Perhaps also a false positive and the standard library tries something different if the first attempt to allocate memory in a certain way failed.
[QUOTE=ZeekyHBomb;41863072]About the heap allocation failure:
Not sure. Perhaps also a false positive and the standard library tries something different if the first attempt to allocate memory in a certain way failed.[/QUOTE]
What puzzles me is why that errors pops up when it works but not when it doesn't work. The font code is executed after the terrain data is loaded, so that code should be executed before it errors out.
Have you tried stripping (seemingly) unrelated parts of the code away from the crashing version?
If the error is on your side, reducing the SLOC to look into should help.
Hello I'm in the progress of making a subnet calculator in C++, I'm having trouble figuring out how I would calculate the subnet mask.
The code below is the calculation of a class A address, as far as I've made it.
[CODE]
void IP::calcA(){
LoopBorrow = 1;
IP_bits = 24.0;
BitMaskValue[0] = 255;
cout << "Please enter the requested number of bits to borrow from the IP address:";
while (LoopBorrow = 1){
cout << "Available bits to borrow: " << IP_bits << "b" << endl << " How many do you wish to borrow?: ";
cin >> Count_bits;
if (Count_bits > 24){
IP_bits -= Count_bits;
UserPerSubnet = pow (2.0, IP_bits);
MaxSubnet = pow (2.0, Count_bits);
if (Count_bits <= 8){
IntegerMask = Count_bits - 8;
switch(IntegerMask){
case 1: BitMaskValue[1] = 1;
case 2: BitMaskValue[1] = 3;
case 3: BitMaskValue[1] = 7;
case 4: BitMaskValue[1] = 15;
case 5: BitMaskValue[1] = 31;
case 6: BitMaskValue[1] = 63;
case 7: BitMaskValue[1] = 128;
case 8: BitMaskValue[1] = 256;
}
}
else if (Count_bits >8 && Count_bits <= 16){
IntegerMask = Count_bits - 16;
switch(IntegerMask){
case 1: BitMaskValue[1] = 1;
case 2: BitMaskValue[1] = 3;
case 3: BitMaskValue[1] = 7;
case 4: BitMaskValue[1] = 15;
case 5: BitMaskValue[1] = 31;
case 6: BitMaskValue[1] = 63;
case 7: BitMaskValue[1] = 128;
case 8: BitMaskValue[1] = 256;
}
}
else if (Count_bits <=17){
IntegerMask = Count_bits - 24;
switch(IntegerMask){
case 1: BitMaskValue[1] = 1;
case 2: BitMaskValue[1] = 3;
case 3: BitMaskValue[1] = 7;
case 4: BitMaskValue[1] = 15;
case 5: BitMaskValue[1] = 31;
case 6: BitMaskValue[1] = 63;
case 7: BitMaskValue[1] = 128;
case 8: BitMaskValue[1] = 256;
}
}
cout << endl << "Subnet Mask: " <<
}
}
}
[/CODE]
[QUOTE=ZeekyHBomb;41863190]Have you tried stripping (seemingly) unrelated parts of the code away from the crashing version?
If the error is on your side, reducing the SLOC to look into should help.[/QUOTE]
I hadn't, no. I just tried though and I've removed everything except the initialising glfw code, the initialising of the camera, the shadermanager and the font code. If I comment out the font code it obviously doesn't crash.
Could I perhaps hit you up on steam or something Zeeky? Might be easier that way if you got the time for helping me out obviously.
[QUOTE=landizz;41863245]Hello I'm in the progress of making a subnet calculator in C++, I'm having trouble figuring out how I would calculate the subnet mask.
The code below is the calculation of a class A address, as far as I've made it.
[CODE]//code[/CODE][/QUOTE]
Stylistic stuff (more or less):
LoopBorrow seems to be an int, while it could be a bool.
If you switch to bool, you don't need the == true part, since the type itself is bool and will be either true or false.
And note that in C++ integers can be implicitly converted to bool: anything not 0 becomes true, 0 becomes false.
It also seems to be a class-member, while it only seems useful within that specific function - why not a local variable? That seems true of most variables used in that snippet.
IP_bits seems to have an inconsistent naming scheme. And why do you assign a double (floating point) value to it? Shouldn't it be an integer type?
Count_bits also seems inconsistent.
Working with IP addresses, which are hexadecimal in nature, you can use hexadecimal integer literals. They're prefixed with 0x, so 255 == 0xFF (or 0xff if you prefer).
You're missing a space after some comparison operators.
Semantic stuff:
The while condition has an assignment to LoopBorrow, you probably meant equality comparison (= vs ==).
Won't stuff get weird when Count_bits is bigger than 24?
You can use bit shifting (<<) instead of pow for this. pow(2,x) == 1 << x.
It also makes more sense to use shifting when working with bit stuff.
I don't completely understand the algorithm you're using to calculate stuff for BitMaskValue[1]. Also in the cases for 7 and 8 in the switches you use even numbers, while the rest are odds.
Perhaps you mean BitMaskValue[1] = (1 << IntegerMask) - 1;?
I think you want the indices 2 and 3 for the else-if branches.
And you're doing redundant checks. If not Count_bits <= 8 (first branch condition), then Count_bits certainly will be > 8 (second branch). And if Count_bits > 8 and not Count_bits <= 16, then Count_bits certainly will be > 17.
IntegerMask also doesn't seem to be much of a bit mask?
Does anyone know of a way to send keyboard events to windows that'll be picked up by games? Currently I'm using keybd_event(VK_CODE, 0, KEYEVENTF_EXTENDEDKEY, 0) which works on the desktop, but doesn't appear to be picked up by games
[QUOTE=ZeekyHBomb;41863478]Stylistic stuff (more or less):
LoopBorrow seems to be an int, while it could be a bool.
If you switch to bool, you don't need the == true part, since the type itself is bool and will be either true or false.
And note that in C++ integers can be implicitly converted to bool: anything not 0 becomes true, 0 becomes false.
It also seems to be a class-member, while it only seems useful within that specific function - why not a local variable? That seems true of most variables used in that snippet.
IP_bits seems to have an inconsistent naming scheme. And why do you assign a double (floating point) value to it? Shouldn't it be an integer type?
Count_bits also seems inconsistent.
Working with IP addresses, which are hexadecimal in nature, you can use hexadecimal integer literals. They're prefixed with 0x, so 255 == 0xFF (or 0xff if you prefer).
You're missing a space after some comparison operators.
Semantic stuff:
The while condition has an assignment to LoopBorrow, you probably meant equality comparison (= vs ==).
Won't stuff get weird when Count_bits is bigger than 24?
You can use bit shifting (<<) instead of pow for this. pow(2,x) == 1 << x.
It also makes more sense to use shifting when working with bit stuff.
I don't completely understand the algorithm you're using to calculate stuff for BitMaskValue[1]. Also in the cases for 7 and 8 in the switches you use even numbers, while the rest are odds.
Perhaps you mean BitMaskValue[1] = (1 << IntegerMask) - 1;?
I think you want the indices 2 and 3 for the else-if branches.
And you're doing redundant checks. If not Count_bits <= 8 (first branch condition), then Count_bits certainly will be > 8 (second branch). And if Count_bits > 8 and not Count_bits <= 16, then Count_bits certainly will be > 17.
IntegerMask also doesn't seem to be much of a bit mask?[/QUOTE]
The LoopBorrow is a bool I just simply use 0,1 instead of false,true.
The names I know I have a problem with, my imagination isn't the best with naming things, especially in a program that contains so much different variables.
Using "<<" instead of pow I didnt know, ty for telling me.
About the algorithm it's beyond done, it's barely started.
My first thought was to make a int array which will keep the seperate 8bits of the mask.
And I thought that if I knew in which range of how many bits that had been borrowed I could take the borrowed bits minus X. Where X = the mask range.
This would give me the remaining bits and therefore I could assemble the second 8 bits of the mask alternative the third or fourth 8bits.
So if I had borrowed lets say 6bits and therefore been in the second octet range I would have 2 bits left in that octet, that gives me 3 if u take that divided by 255 u get your octet of that subnet mask.
If I am not totally wrong here :)
In the switch depending on how many bits left in the IntegerMask a number will come out.
The last two (7,8) of the cases are wrong both should be -1.
The problem is that I'm not sure on how to implement it.
It's generally a good idea to follow the languages idioms. I would suggest you use false and true instead of 0 and 1 for boolean values.
You can many variables in this snippet to a more local scope, where overly specific/descriptive naming is not required. It should still be sufficiently descriptive of course, but with the context of the code around it it should be easier.
What values do you have and what should the result be?
You're getting the number of 1's in the subnet mask as input, right? Let's call that n.
And as output you want the index of octet with the most significant bit that is 0 in that mask? That would be n / 8 (integer arithmetic (truncating)). The number of bits "left" in that octet (i.e. bits that count to the host number) would be 8 - n % 8 (% is modulo, only regarding positive values that gives you the remainder of a division).
I figured out how to do the subnet mask. This works good enough.
Edit: I did not figure it out :D I have some thing to fix before its fully functional.
[CODE]
IP_bits = 24.0;
BitMaskValue[0] = 255, BitMaskValue[1] = 0, BitMaskValue[2] = 0, BitMaskValue[3] = 0;
cout << "Please enter the requested number of bits to borrow from the IP address." << endl;
while (LoopBorrow == true){
cout << "Available bits to borrow: " << IP_bits << "b" << endl << "How many do you wish to borrow?: ";
cin >> Count_bits;
if (Count_bits < 24){
IP_bits -= Count_bits;
UserPerSubnet = pow (2.0, IP_bits) - 2;
MaxSubnet = pow (2.0, Count_bits);
if (Count_bits < 9){
IntegerMask = 8 - Count_bits;
switch(IntegerMask){
case 1: BitMaskValue[1] = 255 - 1; break;
case 2: BitMaskValue[1] = 255 - 3; break;
case 3: BitMaskValue[1] = 255 - 7; break;
case 4: BitMaskValue[1] = 255 - 15; break;
case 5: BitMaskValue[1] = 255 - 31; break;
case 6: BitMaskValue[1] = 255 - 63; break;
case 7: BitMaskValue[1] = 255 - 127; break;
case 8: BitMaskValue[1] = 255 - 255; break;
}
}
else if (Count_bits < 17){
IntegerMask = Count_bits - 8;
switch(IntegerMask){
case 1: BitMaskValue[1] = 255,BitMaskValue[2] = 254; break;
case 2: BitMaskValue[1] = 255,BitMaskValue[2] = 252; break;
case 3: BitMaskValue[1] = 255,BitMaskValue[2] = 248; break;
case 4: BitMaskValue[1] = 255,BitMaskValue[2] = 240; break;
case 5: BitMaskValue[1] = 255,BitMaskValue[2] = 224; break;
case 6: BitMaskValue[1] = 255,BitMaskValue[2] = 192; break;
case 7: BitMaskValue[1] = 255,BitMaskValue[2] = 128; break;
case 8: BitMaskValue[1] = 255,BitMaskValue[2] = 255; break;
}
cout << IntegerMask;
}
else if (Count_bits > 16){
IntegerMask = Count_bits - 16;
switch(IntegerMask){
case 1: BitMaskValue[1] = 255,BitMaskValue[2] = 255 - 1,BitMaskValue[3] = 255 - 1; break;
case 2: BitMaskValue[1] = 255,BitMaskValue[2] = 255 - 1,BitMaskValue[3] = 255 - 3; break;
case 3: BitMaskValue[1] = 255,BitMaskValue[2] = 255 - 1,BitMaskValue[3] = 255 - 7; break;
case 4: BitMaskValue[1] = 255,BitMaskValue[2] = 255 - 1,BitMaskValue[3] = 255 - 15; break;
case 5: BitMaskValue[1] = 255,BitMaskValue[2] = 255 - 1,BitMaskValue[3] = 255 - 31; break;
case 6: BitMaskValue[1] = 255,BitMaskValue[2] = 255 - 1,BitMaskValue[3] = 255 - 63; break;
case 7: BitMaskValue[1] = 255,BitMaskValue[2] = 255 - 1,BitMaskValue[3] = 255 - 127; break;
case 8: BitMaskValue[1] = 255,BitMaskValue[2] = 255 - 1,BitMaskValue[3] = 255; break;
}
}
[/CODE]
I have struct like this:
[cpp]
struct A {
B b;
C c;
}
[/cpp]
Where c keeps a reference to b. Unfortunately, the adress of b doesn't stay constant because I keep A's in a vector (which can move its contents when it resizes). Is there any better solution than resorting to storing a pointer to b instead?
Sorry, you need to Log In to post a reply to this thread.